使用Windows API代码包的CommonOpenFileDialog
时,如何在不获取路径信息的情况下检索filename
的{{1}}(例如file
)?
答案 0 :(得分:3)
使用C#?
string lcMyPath = GetPathFromSomewhere();
string lcMyFilename = Path.GetFileName(lcMyPath);
//-- C:\myfile.txt -> myfile.txt.
Console.WriteLine(lcMyFilename);
答案 1 :(得分:1)
试试这个:http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
// This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''