Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中的组件构建目录字符串#_C#_Filesystems_String Building - Fatal编程技术网

C# 从C中的组件构建目录字符串#

C# 从C中的组件构建目录字符串#,c#,filesystems,string-building,C#,Filesystems,String Building,如果我有很多目录名,或者是文字字符串,或者是包含在变量中,那么最简单的方法是什么来组合这些名称来创建一个完整的路径 我知道Path.Combine,但这只需要2个字符串参数,我需要一个可以接受任意数量目录参数的解决方案 e、 g: 字符串folder1=“foo”; 字符串folder2=“bar”; CreateAPath(“C:,folder1,folder2,folder1,folder1,folder2,MyFile.txt”) 有什么想法吗? C#是否支持方法中的无限参数 C#是否支持

如果我有很多目录名,或者是文字字符串,或者是包含在变量中,那么最简单的方法是什么来组合这些名称来创建一个完整的路径

我知道Path.Combine,但这只需要2个字符串参数,我需要一个可以接受任意数量目录参数的解决方案

e、 g:

字符串folder1=“foo”; 字符串folder2=“bar”; CreateAPath(“C:,folder1,folder2,folder1,folder1,folder2,MyFile.txt”) 有什么想法吗? C#是否支持方法中的无限参数

C#是否支持方法中的无限参数

是的,请查看params关键字。将使编写只调用Path的函数变得简单。组合适当的次数,如下所示(未测试):

试试这个:

public static string CreateDirectoryName(string fileName, params string[] folders)
{
    if(folders == null || folders.Length <= 0)
    {
        return fileName;
    }

    string directory = string.Empty;
    foreach(string folder in folders)
    {
        directory = System.IO.Path.Combine(directory, folder);
    }
    directory = System.IO.Path.Combine(directory, fileName);

    return directory;
}
公共静态字符串CreateDirectoryName(字符串文件名,参数字符串[]文件夹)
{

如果(文件夹=No.x文件夹,长度 LINQ再次进行救援。扩展函数可以用来完成你想要的。考虑这个例子:

string[] ary = new string[] { "c:\\", "Windows", "System" };
string path = ary.Aggregate((aggregation, val) => Path.Combine(aggregation, val));
Console.WriteLine(path); //outputs c:\Windows\System

我更喜欢使用DirectoryInfo而不是Directory上的静态方法,因为我认为这是更好的OO设计。下面是一个使用DirectoryInfo+扩展方法的解决方案,我认为使用它非常好:

    public static DirectoryInfo Subdirectory(this DirectoryInfo self, params string[] subdirectoryName)
    {
        Array.ForEach(
            subdirectoryName, 
            sn => self = new DirectoryInfo(Path.Combine(self.FullName, sn))
            );
        return self;
    }
我不喜欢修改
self
,但是对于这个简短的方法,我认为它比创建一个新变量更简洁

呼叫网站弥补了这一缺陷,不过:

        DirectoryInfo di = new DirectoryInfo("C:\\")
            .Subdirectory("Windows")
            .Subdirectory("System32");

        DirectoryInfo di2 = new DirectoryInfo("C:\\")
            .Subdirectory("Windows", "System32");

添加获取文件信息的方法只是一个练习(对于另一个SO问题!).

请让System.IO.Path方法处理修剪或添加反斜杠之类的事情。好吧,我换一种说法。为什么要重新发明轮子?硬编码的东西对你来说真的更清楚吗?如果你在*NIX系统上运行,正斜杠是路径分隔符怎么办?如果其中一条路径是绝对路径怎么办?Path.Combine句柄同时也是。哦,普通,现在你真的只是重新发明了Path.Combine。它就在那里,所以使用它:投票否决,因为它仍然是关于Path的错误。Combine,仍然会失败于绝对路径,通配符的处理不清楚,并且是一个潜在有效但复杂的解决方案。很抱歉,我不得不坚持这一点,但我不想让它发生站在那里不正确。我们应该这么做的,对吗?我的意图不是要亲自打你。@OregonGhost:+1,但切换“”根据,两者之间没有任何区别,编译器实际上会为两者生成完全相同的IL。但是为了可读性,我会更改它。有人能解释一下我的答案被否决的原因吗?有没有我没有看到的问题?很好。了解可用序列操作的方法!Microsoft做了一个非常好的尝试与LINQ合作。任何时候我需要对收藏做一些有趣的事情,我都会立即去LINQ图书馆查找。
string[] ary = new string[] { "c:\\", "Windows", "System" };
string path = ary.Aggregate((aggregation, val) => Path.Combine(aggregation, val));
Console.WriteLine(path); //outputs c:\Windows\System
    public static DirectoryInfo Subdirectory(this DirectoryInfo self, params string[] subdirectoryName)
    {
        Array.ForEach(
            subdirectoryName, 
            sn => self = new DirectoryInfo(Path.Combine(self.FullName, sn))
            );
        return self;
    }
        DirectoryInfo di = new DirectoryInfo("C:\\")
            .Subdirectory("Windows")
            .Subdirectory("System32");

        DirectoryInfo di2 = new DirectoryInfo("C:\\")
            .Subdirectory("Windows", "System32");