如何在C#中获取安装路径?

huangapple go评论77阅读模式
英文:

how to get the installation path in C#?

问题

我正在修改一个.NET v4.5.2的代码,我想要能够从安装文件夹中获取脚本路径(所以当用户安装应用程序时,应用程序能够访问并运行脚本)。

我尝试了Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)方法,但它返回了错误的值(它返回了路径:"C:\Users\HP\AppData\Roaming",而我的程序位置(安装后)是"C:\Program Files (x86)"。我还尝试了Environment.GetCommandLineArgs()[0]Path.GetDirectoryName(appFileName),但它们返回了代码所在文件的位置。你能帮助我吗?

英文:

I am modifying a .NET v4.5.2 code, and I want to be able to get a script path from the installation folder (so when a user install the app, the app is able to access and run the script)

I tried Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) method but it returned the wrong value (it returned the path: "C:\Users\HP\AppData\Roaming" , whereas my program location (after installation) is "C:\Program Files (x86)". I also tried Environment.GetCommandLineArgs()[0] and Path.GetDirectoryName(appFileName) but they returned the location of the file where the code was written. could you please help me?

答案1

得分: 1

使用以下代码获取应用程序域的基本目录:

String _path = System.AppDomain.CurrentDomain.BaseDirectory;

你也可以查看 AppDomain.BaseDirectory() 的文档了解更多信息。

英文:

try AppDomain.BaseDirectory()

String _path = System.AppDomain.CurrentDomain.BaseDirectory;

huangapple
  • 本文由 发表于 2023年2月9日 01:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定