I am building an application in c# and i need 开发者_JAVA百科to get the name of a file without knowing his path (browser) (not a web app not asp), so later i could change his name. how can i do that
thanks for all the help
Well, I assume you are using OpenFileDialog to
"show me all my files and i will Select the file i want"
You can get the file name (path) of the file you have selected using OpenFileDialog's FileName
property. To get only file name + extension use the Path class function GetFileName()
.
Here is some code
using (OpenFileDialog dg = new OpenFileDialog())
{
if (dg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dg.FileName;
string name = Path.GetFileName(path);
}
}
Assuming from your comments that you want to open a file browsing dialog, then it depends upon which framework you are using.
If you are using Forms, then you can use the Forms FileDialog.
If you are using WPF, then you can use the Win32 OpenFileDialog.
As I recall, these dialogs will probably let you rename files directly within them (e.g. select the file and hit F2).
精彩评论