我想知道,以确保将从我的脚本下载的文件具有我想要的扩展名。
该文件不会位于以下 URL 处:
http://example.com/this_url_will_download_a_file
或者也许是的,但是,我认为我只会使用这种 URL:
http://example.com/file.jpg
我不会检查它:
Url.Substring(Url.LastIndexOf(".") - 3, 3)
因为这是一种非常糟糕的方式。那么,你建议我做什么?
最佳答案
这很奇怪,但它有效:
string url = @"http://example.com/file.jpg";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);
但正如 crono 在下面所说的,它不适用于参数:string url = @"http://example.com/file.jpg?par=x";
string ext = System.IO.Path.GetExtension(url);
MessageBox.Show(this, ext);
结果:“.jpg?par=x”
关于.net - 有没有办法从 URL 获取文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23228378/