c# 为什么当路径是“C:”时DirectoryInfo 将我带到应用程序文件夹?

发布于 2025-01-02 08:56:38 字数 565 浏览 0 评论 0原文

为什么当我给出路径“c:”时它直接将我更改为应用程序文件夹?

    static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo("c:");
        Console.WriteLine(dir.FullName);
        Console.ReadLine();
    }

输出如下:

c:\users...\documents\Visual Studio 2010\projects\consoleApplication9\bin\debug

但是当我给出 @"c:\" 时,它会转到磁盘 c: 尽管 "d:"@"d:\" 会写入磁盘 d:

所以我需要一种方法让 "c:" 保存到磁盘 c:

提前致谢!

Why when i give the path "c:" it changed me directly to application folder?

    static void Main(string[] args)
    {
        DirectoryInfo dir = new DirectoryInfo("c:");
        Console.WriteLine(dir.FullName);
        Console.ReadLine();
    }

The output is the following:

c:\users...\documents\visual studio
2010\projects\consoleApplication9\bin\debug

But when I give @"c:\" it goes to disk c:
despite that "d:" and @"d:\" takes to disk d:.

So I need a way to let "c:" takes to disk c:

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2025-01-09 08:56:38

"c:" 表示“C 驱动器上的当前目录”,而 @"c:\" 表示“C 驱动器的根目录”。这与命令提示符下的工作方式相同......

Just "c:" means "the current directory on the C drive" whereas @"c:\" means "root of the C drive". This works the same way from a command prompt...

欢你一世 2025-01-09 08:56:38

C: 只是卷说明符,因此它将更改为该卷上的当前路径,这将是应用程序的工作路径。

D:将您带到根目录只是因为该卷的当前文件夹恰好位于根目录。

C: is just the volume specifier, so it will change to your current path on that volume, which would be the working path of the application.

D: takes you to root simply because your current folder for that volume happens to be at root.

甜味超标? 2025-01-09 08:56:38
static void Main(string[] args) 
    { 
        string YourDir = "c:";

        if (!YourDir.Substring(YourDir.Length - 1, 1).Equals(@"\"))
            YourDir += @"\";
        DirectoryInfo dir = new DirectoryInfo(YourDir); 
        Console.WriteLine(dir.FullName); 
        Console.ReadLine(); 
    } 
static void Main(string[] args) 
    { 
        string YourDir = "c:";

        if (!YourDir.Substring(YourDir.Length - 1, 1).Equals(@"\"))
            YourDir += @"\";
        DirectoryInfo dir = new DirectoryInfo(YourDir); 
        Console.WriteLine(dir.FullName); 
        Console.ReadLine(); 
    } 
℡Ms空城旧梦 2025-01-09 08:56:38

在此处输入图像描述

使用以下

  static void Main(string[] args)
  {          
      DirectoryInfo dir = new DirectoryInfo(@"c:\");
      Console.WriteLine(dir.FullName);
      Console.ReadLine();
  }       

在执行 c 时 基本目录:应用程序不理解这一点,因此它返回应用程序启动/运行的目录。

请注意 dir = {.} 如果您传入文字目录路径,您将获得预期的结果。

enter image description here

Use the following

  static void Main(string[] args)
  {          
      DirectoryInfo dir = new DirectoryInfo(@"c:\");
      Console.WriteLine(dir.FullName);
      Console.ReadLine();
  }       

The Base Directory at the time when you do c: the application doesn't understand that so it returns the Directory of where the application was launched / run from.

Notice that dir = {.} if you would have passed in a literal directory path you would have gotten the expected results..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文