在 .NET 控制台应用程序中,如何获取用户键入命令的命令提示符路径?

In .NET console app, how to get the path of the command prompt where the user typed the command?

我创建并构建了以下 .NET 控制台应用程序,并将可执行文件 deepdir.exe 复制到 c:\commandlineapps,然后将环境变量设置到此目录,以便我可以从任何地方调用此命令目录。

如何从用户键入命令的地方获取目录,例如c:\docs\project1,而不是 .exe 文件所在的目录,例如c:\commandlineapps? None 以下作品:

using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;

namespace showimages
{
    class Program
    {
        static void Main(string[] args)
        {
            var docPath = AppDomain.CurrentDomain.BaseDirectory;
            docPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            docPath = Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
            docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            docPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            docPath = Environment.CurrentDirectory;
            docPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            docPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            docPath = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
            docPath = System.AppContext.BaseDirectory;
            docPath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            Console.WriteLine(docPath);
        }
    }
}       

Directory.GetCurrentDirectory() 会做你需要的(获取当前工作目录)。