如何删除路径的DirectorySeparatorChar? [英] How do I remove the DirectorySeparatorChar of an Path?

查看:54
本文介绍了如何删除路径的DirectorySeparatorChar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我目前有一点问题,因为系统无法识别以反斜杠结尾的路径



我在这里尝试这个,但它对论证没有影响

Hi guys.

I have currently a little Problem, because the System doesn't recognizes a Path that ends with a backslash

I'm trying this here, but it has no affect on the argument

args[0] = args[0].TrimEnd(Path.DirectorySeparatorChar);





谢谢。



真诚地:

Jonas



thanks.

Sincerely:
Jonas

推荐答案

系统无法识别以反斜杠结尾的路径

是的,确实如此:它说这是一个文件夹。

"the System doesn't recognizes a Path that ends with a backslash"
Yes, it does: It says "this is a folder".
static void Main(string[] args)
    {
    string[] files = Directory.GetFiles(args[0]);
    foreach (string s in files) Console.WriteLine(s);
    args[0] = args[0].TrimEnd(Path.DirectorySeparatorChar);
    foreach (string s in args)
        Console.WriteLine(s);
    }



然后使用ConsoleTester D:\ temp \运行应用程序,它会打印临时文件列表文件夹。



但是......请注意,Path.DirectorySeparatorChar只是反斜杠 - 它也不会与斜杠'/'匹配,但是Windows很满意。



如果您的路径以分隔符结尾,则它是文件夹路径,而不是文件路径。所以File.RealAllLines(等等)不起作用 - 它们需要一个文件,而不是一个文件夹。


Then run the app with "ConsoleTester D:\temp\", and it prints the list of files in my temporary folder.

But...Do note that Path.DirectorySeparatorChar is only backslash - it will not match a slash '/' as well, but Windows is happy with either.

And if your path ends with a separator character, it's a folder path, not a file path. So File.RealAllLines (and so forth) will not work - they need a file, not a folder.


这篇关于如何删除路径的DirectorySeparatorChar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆