我创建了以下.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);
}
}
}