如何从乌里在C#中提取文件名?
例如,我有一个开放的
"http://audacity.googlecode.com/files/audacity-win-2.0.exe"
但如何提取文件名
"audacity-win-2.0.exe"
,并保存为一个字符串?
谢谢
杰里
如何从乌里在C#中提取文件名?
例如,我有一个开放的
"http://audacity.googlecode.com/files/audacity-win-2.0.exe"
但如何提取文件名
"audacity-win-2.0.exe"
,并保存为一个字符串?
谢谢
杰里
如何
Path.GetFileName(string path);
在你的情况,你应该首先检查是否是一个文件。
Uri u = new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe")
string filename = string.Empty;
if (u.IsFile)
filename = Path.GetFileName(u.AbsolutePath);
它会给你没事文件名。 – scegg
这可能是旧的,但对于这个例子,这个方法在文件名上会得到空字符串,因为当你给一个文件时IsFile只有true:// type URI –
它是不安全的假设URI
总会代表物理文件,因此从URI
解压文件名并不总是保证。
例如,
Uri uri = new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe");
string Path.GetFileName(uri.AbsolutePath);
Path.GetFileName
可以做到这一点...
Uri u = new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe");
Path.GetFileName(u.AbsolutePath);
见我的回答http://stackoverflow.com/a/40361205/64334 –