OpenFileDialog !

  • Thread starter Thread starter C# newbie
  • Start date Start date
Hello,

using openfiledialog, how can I retrieve the path ?

thx

OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() == DialogResult.OK)
{
string fn = Path.GetFileName(o.FileName);
string path = Path.GetDirectoryName(o.FileName);
}
 
Hi there,

First add a reference to System.Design.dll and then the following code
should work:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class BrowseForFolder : FolderNameEditor
{
FolderNameEditor.FolderBrowser bDialog;

public BrowseForFolder()
{
bDialog = new FolderNameEditor.FolderBrowser();
}

public string browseDialog(string sTitle)
{
bDialog.Description = sTitle;
bDialog.StartLocation =
FolderNameEditor.FolderBrowserFolder.MyComputer;
bDialog.Style = FolderNameEditor.FolderBrowserStyles.RestrictToDomain;
bDialog.ShowDialog();
return bDialog.DirectoryPath;
}
~BrowseForFolder()
{
bDialog.Dispose();
}
}

This code is from here:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=275&lngW
Id=10

The article says you can call the code like so:

BrowseForFolder myDialog = new BrowseForFolder();
MessageBox.Show(myDialog.browseDialog("Dialog Title Goes Here");

But if you want to be able to determine what the user pressed, you can do
something like the following:

DialogResult result = myDialog.ShowDialog()

You can find more about the DialogResult at:
http://msdn.microsoft.com/library/default.asp?url=/library/enus/cpref/html/f
rlrfsystemwindowsformsdialogresultclasstopic.asp

Good luck!

Mack D. Male
MVP Visual Developer - Academic


Hello,

using openfiledialog, how can I retrieve the path ?

thx
 
Thanks. The thing is I'm useing the path either but compiler complains on
path!
Any suggestions ?

Regards
 
Thanks. The thing is I'm useing the path either but compiler complains on
path!
Any suggestions ?

Regards

I'm sorry, I'm afraid I don't understand your question?
 
Hi Ludwig,

Thanks for your quick response. Regarding your code :

OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() == DialogResult.OK)
{
string fn = Path.GetFileName(o.FileName);
string path = Path.GetDirectoryName(o.FileName);
}

the compiler complains about "Path". Also, after adding a dot to "Path"
Intellisence does't come upand doesn't show any method related. I was
assuming that Path is a member of the OpenFileDialog class.

Any idea?

Thanks in advance
 
Hi Ludwig,

Thanks for your quick response. Regarding your code :

OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() == DialogResult.OK)
{
string fn = Path.GetFileName(o.FileName);
string path = Path.GetDirectoryName(o.FileName);
}

the compiler complains about "Path". Also, after adding a dot to "Path"
Intellisence does't come upand doesn't show any method related. I was
assuming that Path is a member of the OpenFileDialog class.

Any idea?

No, Path is a System.IO class, so use System.IO.Path, or put "using
System.IO;" on top.
 
Back
Top