Solved

parsing string vc++

Posted on 2009-05-14
8
260 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
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

728 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