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

时间:2019-05-23 12:47:45

标签: c# windows console-application

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

如何从用户键入命令的位置获取目录,例如c:\docs\project1,而不是.exe文件所在的目录,例如c:\commandlineapps?没有以下工作:

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);
        }
    }
}       

1 个答案:

答案 0 :(得分:1)

Directory.GetCurrentDirectory()将完成您需要的操作(获取当前工作目录)。