Solved

parsing string vc++

Posted on 2009-05-14
8
259 Views
Last Modified: 2013-12-14
I have strig with filename from openFileDialog. I want to extract folder path out of it. Something like
c:\project\folder\filename
and i want to get
c:\project\folder
what is a good way of going this? I am using VS2008 and windows forms appl.
0
Comment
Question by:funcoding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24387425
Is this for C++ or C#?
0
 
LVL 1

Author Comment

by:funcoding
ID: 24387449
vc++
0
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24387479
Not sure what's the syntax for VC++ but here's how you do it with C#.
This shoud help you since the classes are the same.
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
string filename = ofd.FileName;
string path = Path.GetDirectoryName(ofd.FileName);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:funcoding
ID: 24387567
what is Path in there? "Path.GetDirectoryName"
0
 
LVL 8

Accepted Solution

by:
RyanAndres earned 125 total points
ID: 24387697
System.IO.Path
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 24387993
try

 
private:
    FileStream* OpenFile() {
        // Displays an OpenFileDialog and shows the read/only files.
 
        OpenFileDialog* dlgOpenFile = new OpenFileDialog();
        dlgOpenFile->ShowReadOnly = true;
 
        if (dlgOpenFile->ShowDialog() == DialogResult::OK) {
           String* path = dlgOpenFile->FileName;
           String* folder = path->Substring(0, path->LastIndexOfAny(L"\\/"));
           ...
        }
        return 0;
    }

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24390439
You can't use pointers with Managed types; instead, Microsoft has provided us with yet another indirection operator indicating a handle to a managed type, which is "^". To make the above code work, do the following:

First, change all asterisks (*) to ^.

Second, change "new" OpenFileDialog to "gcnew"

Hope I helped,
Nate
0
 
LVL 1

Author Comment

by:funcoding
ID: 24391570
I completely forgot to update that RyanAdnre's suggestion had worked. Thanks everyone else for input also.

String^ filename = openFileDialog->filename;
String^ folderpath = System::IO::Path::GetDirectoryName(filename);
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question