在C#中,我们有以下几种方式获取程序当前目录:
Console.WriteLine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory); Console.WriteLine(System.Environment.CurrentDirectory); Console.WriteLine(System.IO.Directory.GetCurrentDirectory()); Console.WriteLine(Environment.CurrentDirectory);
输出:
D:\MyTool\MyWorkRelated\C#_Tool\Defect Process Tool\DefectExcelProcesTool\DefectExcelProcesTool\bin\Debug
D:\MyTool\MyWorkRelated\C#_Tool\Defect Process Tool\DefectExcelProcesTool\DefectExcelProcesTool\bin\Debug\
D:\MyTool\MyWorkRelated\C#_Tool\Defect Process Tool\DefectExcelProcesTool\DefectExcelProcesTool\bin\Debug
D:\MyTool\MyWorkRelated\C#_Tool\Defect Process Tool\DefectExcelProcesTool\DefectExcelProcesTool\bin\Debug
D:\MyTool\MyWorkRelated\C#_Tool\Defect Process Tool\DefectExcelProcesTool\DefectExcelProcesTool\bin\Debug
亲测可以代码正确。