c#更改相对或绝对uri的文件名

标签 c# .net url

我正在寻找一种干净的方法来更改可能包含多个段的绝对或相对 uri 的文件名。我们不知道它是绝对的还是相对的,所以我们需要检测:

这是测试用例:

    [Test]
    public void Can_change_path_file_name()
    {
        Fix("file.txt").ShouldEqual("file_fixed.txt");
        Fix("/file.txt").ShouldEqual("/file_fixed.txt");
        Fix("directory/file.txt").ShouldEqual("directory/file_fixed.txt");
        Fix("/directory/file.txt").ShouldEqual("/directory/file_fixed.txt");
        Fix("/directory/subdirectory/file.txt").ShouldEqual("/directory/subdirectory/file_fixed.txt");
        Fix("http://www.test.com/directory/subdirectory/file.txt").ShouldEqual("http://www.test.com/directory/subdirectory/file_fixed.txt");
        Fix("/directory/file/file.txt").ShouldEqual("/directory/file/file_fixed.txt");
    }

    private string Fix(string uri)
    {
       // fill me with goodness
    }

我的解决方案有效,但似乎有点复杂。如果您想改进它,我已将其发布在下面:

private string Fix(string uri)
{
    var fileName = Path.GetFileNameWithoutExtension(uri);
    var extension = Path.GetExtension(uri);
    fileName += "_fixed" + extension;

    var path = uri.Contains('/') ? uri.Substring(0, uri.LastIndexOf('/') + 1) : "";
    return Combine(path, fileName);
}

private string Combine(string path1, string path2)
{
    if (path1 == null)
    {
        throw new ArgumentNullException("path1");
    }

    if (path2 == null)
    {
        throw new ArgumentNullException("path2");
    }

    if (String.IsNullOrEmpty(path2))
    {
        return path1;
    }

    if (String.IsNullOrEmpty(path1))
    {
        return path2;
    }

    if (path2.StartsWith("http://") || path2.StartsWith("https://"))
    {
        return path2;
    }

    var ch = path1[path1.Length - 1];

    if (ch != '/')
    {
        return (path1.TrimEnd('/') + '/' + path2.TrimStart('/'));
    }

    return (path1 + path2);
}

最佳答案

没有正则表达式,没有字符串索引。

只需使用 .NET Path 方法来检索并重新组合一个新文件名:

/// <summary>
/// Partner to Path.ChangeExtension. This function changes the base filename portion
/// </summary>
/// <param name="path"></param>
/// <param name="newFilename"></param>
/// <returns></returns>
public static String ChangeFilename(String path, String newFilename)
{
   String directoryName = Path.GetDirectoryName(path); //e.g. "C:\Temp\foo.dat" ==> "C:\Temp"
   //String oldFilename = Path.GetFileName(path); //e.g. "C:\Temp\foo.dat" ==> "foo.dat"
   //String filenameWithoutExtension = Path.GetFileNameWithoutExtension(path); //e.g. "C:\Temp\foo.dat" ==> "foo"
   String extension = Path.GetExtension(path); //e.g. "C:\Temp\foo.dat" ==> ".dat"

   //Reassemble as
   //    directoryName \ newFilename dotExtension
   return String.Format("{0}{1}{2}{3}",
         directoryName, 
         Path.DirectorySeparatorChar,
         newFilename,
         extension);
}

Note: Any code is released into the public domain. No attribution required.

关于c#更改相对或绝对uri的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710518/

相关文章:

c# - 为什么我不应该在 ASP.NET MVC 3 中使用 TempData、Session 和 ViewBag?

.net - Internet Explorer 在查询字符串中遇到特殊字符问题

c# - 根据内容对文件内容进行排序

c# - 是否可以在同一应用程序中同时使用 IronPython 和 IronRuby?

.net - 如何以编程方式创建具有强名称的 .NET 程序集?

c# - 对象序列化上下文中的 TYPE FIDELITY 是什么?

c# - Linq动态添加where条件

c# - EF 扩展每个案例的查询

Android 自动链接以启动 WebView

url - 如何在 opencart 中制作自定义 SEO url?