获取程序运行的目录。例如D:\Program Files\QQ.exe,则可以获取D:\Program Files
string method1 = AppDomain.CurrentDomain.BaseDirectory; //D:\Test\bin\Debug\ System.IO.FileInfo info = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location); string method2 = info.Directory.FullName; //D:\Test\bin\Debug string method3 = Application.StartupPath; //D:\Test\bin\Debug
注意上面的结果有少许差别:第一种最后还会有'\'
调取电脑的默认安装路径:
using Microsoft.Win32; RegistryKey Key = Registry.LocalMachine; RegistryKey ServiceIP = Key.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion", false); string strServiceIP = ServiceIP.GetValue("ProgramFilesDir").ToString();