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.
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.
Is this for C++ or C#?
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.
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);
ASKER
what is Path in there? "Path.GetDirectoryName"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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
First, change all asterisks (*) to ^.
Second, change "new" OpenFileDialog to "gcnew"
Hope I helped,
Nate
ASKER
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::GetDirec toryName(f ilename);
String^ filename = openFileDialog->filename;
String^ folderpath = System::IO::Path::GetDirec