C# 某些Python函数的.NET等价物

C# 某些Python函数的.NET等价物,c#,python,porting,C#,Python,Porting,我正在尝试将一些Python代码移植到.NET,我想知道在.NET中是否有与下列Python函数等价的函数,或者一些具有相同功能的代码片段 os.path.split() os.path.basename() 编辑 Python中的os.path.basename()返回os.path.split的尾部,而不是System.IO.path.GetPathRoot(path)的结果 我认为下面的方法为os.path.split函数创建了一个合适的端口,欢迎进行任何调整。我相信,它尽可能遵循os.

我正在尝试将一些Python代码移植到.NET,我想知道在.NET中是否有与下列Python函数等价的函数,或者一些具有相同功能的代码片段

os.path.split()
os.path.basename()
编辑

Python中的os.path.basename()返回os.path.split的尾部,而不是System.IO.path.GetPathRoot(path)的结果

我认为下面的方法为os.path.split函数创建了一个合适的端口,欢迎进行任何调整。我相信,它尽可能遵循os.path.split的描述

    public static string[] PathSplit(string path)
    {
        string head = string.Empty;
        string tail = string.Empty;

        if (!string.IsNullOrEmpty(path))
        {
            head = Path.GetDirectoryName(path);
            tail = path.Replace(head + "\\", "");
        }

        return new[] { head, tail };
    }

我不确定返回head和tail的方式,因为我真的不想通过参数将head和tail传递给方法。

您正在查找
System.IO.Path

它有许多功能,您可以使用它们来获得相同的功能

os.path.split()
os.path.basename()
  • Path.GetDirectoryName(字符串)
  • 对于split,您可能希望在实际路径名上使用
    String.split(…)
    。您可以通过
    Path.PathSeparator
    获得依赖于操作系统的分隔符
  • 如果我错过了关于
    os.path.split
    的要点,而您需要文件名,请使用
    path.GetFileName(string)
请注意:您可以使用Visual Studio中的对象浏览器(Ctrl+Alt+J),浏览
System.IO
命名空间的所有成员。从这里开始
mscorlib
->
System.IO
,所有类都将在那里被发现


这就像破解上的智能感知:)

您正在寻找
System.IO.Path

它有许多功能,您可以使用它们来获得相同的功能

os.path.split()
os.path.basename()
  • Path.GetDirectoryName(字符串)
  • 对于split,您可能希望在实际路径名上使用
    String.split(…)
    。您可以通过
    Path.PathSeparator
    获得依赖于操作系统的分隔符
  • 如果我错过了关于
    os.path.split
    的要点,而您需要文件名,请使用
    path.GetFileName(string)
请注意:您可以使用Visual Studio中的对象浏览器(Ctrl+Alt+J),浏览
System.IO
命名空间的所有成员。从这里开始
mscorlib
->
System.IO
,所有类都将在那里被发现

这就像破解上的智能感知:)

Path.GetFileName Path.GetDirectoryName

应该有帮助

Path.GetFileName Path.GetDirectoryName

应该有帮助

os.path.basename()

另一种选择是
System.IO.Path.GetPathRoot(路径)

Edit:上面返回了路径的第一个路径,其中
basename
应该返回路径的尾部。有关如何实现这一点的示例,请参见下面的代码

os.path.split()

不幸的是,由于没有与.Net等效的版本,因此没有其他替代方案。你能找到的最接近的路径是
System.IO.Path.GetDirectoryName(Path)
,但是如果你的路径是C:\Foo,那么
GetDirectoryName
会给你C:\Foo,而不是C:Foo。只有当您想要获取实际文件路径的目录名时,这才有效

因此,您必须编写如下代码来分解这些代码:

public void EquivalentSplit(string path, out string head, out string tail)
{

    // Get the directory separation character (i.e. '\').
    string separator = System.IO.Path.DirectorySeparatorChar.ToString();

    // Trim any separators at the end of the path
    string lastCharacter = path.Substring(path.Length - 1);
    if (separator == lastCharacter)
    {
        path = path.Substring(0, path.Length - 1);
    }

    int lastSeparatorIndex = path.LastIndexOf(separator);

    head = path.Substring(0, lastSeparatorIndex);
    tail = path.Substring(lastSeparatorIndex + separator.Length,
        path.Length - lastSeparatorIndex - separator.Length);

}
os.path.basename()

另一种选择是
System.IO.Path.GetPathRoot(路径)

Edit:上面返回了路径的第一个路径,其中
basename
应该返回路径的尾部。有关如何实现这一点的示例,请参见下面的代码

os.path.split()

不幸的是,由于没有与.Net等效的版本,因此没有其他替代方案。你能找到的最接近的路径是
System.IO.Path.GetDirectoryName(Path)
,但是如果你的路径是C:\Foo,那么
GetDirectoryName
会给你C:\Foo,而不是C:Foo。只有当您想要获取实际文件路径的目录名时,这才有效

因此,您必须编写如下代码来分解这些代码:

public void EquivalentSplit(string path, out string head, out string tail)
{

    // Get the directory separation character (i.e. '\').
    string separator = System.IO.Path.DirectorySeparatorChar.ToString();

    // Trim any separators at the end of the path
    string lastCharacter = path.Substring(path.Length - 1);
    if (separator == lastCharacter)
    {
        path = path.Substring(0, path.Length - 1);
    }

    int lastSeparatorIndex = path.LastIndexOf(separator);

    head = path.Substring(0, lastSeparatorIndex);
    tail = path.Substring(lastSeparatorIndex + separator.Length,
        path.Length - lastSeparatorIndex - separator.Length);

}

为什么不使用IronPython并充分利用这两个世界呢

为什么不使用IronPython并充分利用这两个世界

AFAIK
os.path.split()
String.split()
不同
os.path.split()
将路径分解为两部分(头部和尾部)。例如,C:/Folder/File.txt将被分解为C:/Folder和File.txt。但我可能错了,因为我可能误解了文件。我不是python程序员。@GenericTypeTea:Hrm你可能是对的。我已经有一段时间没有用python做任何事情了。我将修改我的答案。GetFileName仍然与os.path不相似。split()还将返回路径中的最后一个文件夹。例如,C:\Foo\Bar将是C:\Foo,Bar。@GenericTypeTea:没有直接等价于
os.path.split()。您必须使用
.GetFileName(string)
.GetDirectoryName(string)
来获取这两部分。
os.path.split()
提供了路径的头和尾。它不仅仅给你文件名。如果没有文件名,那么它会给出路径中的最后一个文件夹。也就是说,正如我在前面的评论中所说,它应该返回示例C:\Foo\Bar中的head:C:\Foo和tail:Bar。因此,GetFileName不是等效的。AFAIK
os.path.split()
String.split()
不同
os.path.split()
将路径分解为两部分(头部和尾部)。例如,C:/Folder/File.txt将被分解为C:/Folder和File.txt。但我可能错了,因为我可能误解了文件。我不是python程序员。@GenericTypeTea:Hrm你可能是对的。我已经有一段时间没有用python做任何事情了。我