我有这个方法从字符串URI中获取文件名。我该怎么做才能让它更健壮呢?

private string GetFileName(string hrefLink)
{
    string[] parts = hrefLink.Split('/');
    string fileName = "";

    if (parts.Length > 0)
        fileName = parts[parts.Length - 1];
    else
        fileName = hrefLink;

    return fileName;
}

当前回答

Uri。IsFile不能使用http url。它只适用于“file://”。 当Scheme属性等于ur缺血efile时,IsFile属性为真。 所以你不能依赖它。

Uri uri = new Uri(hreflink);
string filename = System.IO.Path.GetFileName(uri.LocalPath);

其他回答

这是我的样本,你可以使用:

        public static string GetFileNameValidChar(string fileName)
    {
        foreach (var item in System.IO.Path.GetInvalidFileNameChars())
        {
            fileName = fileName.Replace(item.ToString(), "");
        }
        return fileName;
    }

    public static string GetFileNameFromUrl(string url)
    {
        string fileName = "";
        if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
        {
            fileName = GetFileNameValidChar(Path.GetFileName(uri.AbsolutePath));
        }
        string ext = "";
        if (!string.IsNullOrEmpty(fileName))
        {
            ext = Path.GetExtension(fileName);
            if (string.IsNullOrEmpty(ext))
                ext = ".html";
            else
                ext = "";
            return GetFileNameValidChar(fileName + ext);

        }

        fileName = Path.GetFileName(url);
        if (string.IsNullOrEmpty(fileName))
        {
            fileName = "noName";
        }
        ext = Path.GetExtension(fileName);
        if (string.IsNullOrEmpty(ext))
            ext = ".html";
        else
            ext = "";
        fileName = fileName + ext;
        if (!fileName.StartsWith("?"))
            fileName = fileName.Split('?').FirstOrDefault();
        fileName = fileName.Split('&').LastOrDefault().Split('=').LastOrDefault();
        return GetFileNameValidChar(fileName);
    }

用法:

var fileName = GetFileNameFromUrl("http://cdn.p30download.com/?b=p30dl-software&f=Mozilla.Firefox.v58.0.x86_p30download.com.zip");

接受的答案是有问题的http url。此外Uri。LocalPath执行Windows特定的转换,正如有人指出的那样,它将查询字符串留在那里。更好的方法是使用Uri。AbsolutePath

正确的方法来做这个http url是:

Uri uri = new Uri(hreflink);
string filename = System.IO.Path.GetFileName(uri.AbsolutePath);

截至2020年,处理查询字符串和编码的url

public static string GetFileNameFromUrl (string url)
{
    var decoded = HttpUtility.UrlDecode(url);

    if (decoded.IndexOf("?") is {} queryIndex && queryIndex != -1)
    {
        decoded = decoded.Substring(0, queryIndex);
    }

    return Path.GetFileName(decoded);
}

Uri。IsFile不能使用http url。它只适用于“file://”。 当Scheme属性等于ur缺血efile时,IsFile属性为真。 所以你不能依赖它。

Uri uri = new Uri(hreflink);
string filename = System.IO.Path.GetFileName(uri.LocalPath);
  //First Method to get fileName from fileurl  


 List<string> fileNameListValues = new List<string>();  

//fileNameListValues List  consist of fileName and fileUrl 
//we need to get fileName and fileurl from fileNameListValues List 
 name 

 foreach(var items in fileNameListValues)
 {
var fileUrl = items;
var uriPath = new Uri(items).LocalPath;
var fileName = Path.GetFileName(uriPath);
 }


  //Second Way to get filename from fileurl is ->      


 fileNameValue = "https://projectname.com/assets\UploadDocuments\documentFile_637897408013343662.jpg";
 fileName = " documentFile_637897408013343662.jpg";

 //way to get filename from fileurl 

 string filename = 
  fileNameValue.Substring(fileNameValue.LastIndexOf("\\") + 1);