Link to home
Start Free TrialLog in
Avatar of funcoding
funcoding

asked on

parsing string vc++

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.
Avatar of RyanAndres
RyanAndres
Flag of United States of America image

Is this for C++ or C#?
Avatar of funcoding
funcoding

ASKER

vc++
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

what is Path in there? "Path.GetDirectoryName"
ASKER CERTIFIED SOLUTION
Avatar of RyanAndres
RyanAndres
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
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);