Solved

parsing string vc++

Posted on 2009-05-14
8
250 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
 
LVL 1

Author Comment

by:funcoding
ID: 24387567
what is Path in there? "Path.GetDirectoryName"
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

758 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

21 Experts available now in Live!

Get 1:1 Help Now