C++/CLI获取可执行程序路径的方法
以下资料参考http://blog.sina.com.cn/s/blog_49947b2801009gcj.html,只是为了记忆,所以自己再写一遍。
1 System::Diagnostics::Process::GetCurrentProcess()->MainModule->FileName,获取的是Module的文件名;
Example:
string str = System::Diagnostics::Process::GetCurrentProcess()->MainModule->FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
2 System::Environment::CurrentDirectory和System::IO::Directory::GetCurrentDirectory,这两个方法获取当前路径,任何会改变当前路径的方法都会改变它。
Example:
string str = System::Environment::CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录,不带"\")
3 System::AppDomain::CurrentDomain->BaseDirectory和System::AppDomain::CurrentDomain->SetupInformation->ApplicationBase,这两种方法获取的路径很可靠,以"\"结尾。
Example:
string str = System::AppDomain::CurrentDomain->BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
4 System::Windows::Forms::Application::StartupPath,也能可靠的获取可执行程序的路径,不过没有以"\"结尾。
Example:
string str = System::Windows::Forms::Application::StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)
5 System::Windows::Forms::Application::ExecutablePath,此方法也能可靠的获取路径,但也包括可执行程序文件名。
Example:
string str = System::Windows::Forms::Application::ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

浙公网安备 33010602011771号