Solved

parsing string vc++

Posted on 2009-05-14
8
256 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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…
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.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now