C# 獲取當前路徑7種方法

        //獲取模塊的完整路徑。
            string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            //獲取和設置當前目錄(該進程從中啓動的目錄)的徹底限定目錄
            string path2 = System.Environment.CurrentDirectory;
            //獲取應用程序的當前工做目錄
            string path3 = System.IO.Directory.GetCurrentDirectory();
            //獲取程序的基目錄
            string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
            //獲取和設置包括該應用程序的目錄的名稱
            string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //獲取啓動了應用程序的可執行文件的路徑
            string path6 = System.Windows.Forms.Application.StartupPath;
            //獲取啓動了應用程序的可執行文件的路徑及文件名
            string path7 = System.Windows.Forms.Application.ExecutablePath;
 
            StringBuilder str=new StringBuilder();
            str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1);
            str.AppendLine("System.Environment.CurrentDirectory:" + path2);
            str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3);
            str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4);
            str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5);
            str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6);
            str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7);
            string allPath = str.ToString();
 
            /*  輸出結果
             *  System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
                System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
                System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
                System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
                System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
                System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
                System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE
相關文章
相關標籤/搜索