英文:
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() 的文档了解更多信息。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论