[英]How should I configure paths to other exe files in my project?
I am working on a project that requires a .exe file to be executed by the application. 我正在一个需要由应用程序执行的.exe文件的项目中。 The problem here is that I cannot guarantee that the file will always be in the same location on the user's machine, my app has no way of knowing where this .exe file is located.
这里的问题是我不能保证该文件将始终位于用户计算机上的同一位置,我的应用程序无法知道此.exe文件的位置。
So for example, my application needs to execute mongod.exe which is located (by default) in the following location: C:\\Program Files\\MongoDB\\Server\\3.2\\bin\\mongod.exe . 因此,例如,我的应用程序需要执行mongod.exe (默认情况下位于以下位置): C:\\ Program Files \\ MongoDB \\ Server \\ 3.2 \\ bin \\ mongod.exe 。
The problem here is that this is not always the case, the user might have installed this application to a different place, so I can't assume that the path to the .exe file is always the same on every client. 这里的问题是,情况并非总是如此,用户可能已将此应用程序安装到了其他位置,所以我不能假定.exe文件的路径在每个客户端上都相同。
Currently I am using the Settings.settings file to store a default file path to the file: 当前,我正在使用Settings.settings文件存储该文件的默认文件路径:
The user can then either edit the app.config file or I could provide a UI to allow the user to change the file path. 然后,用户可以编辑app.config文件,也可以提供一个UI以允许用户更改文件路径。
So my question is, is there a better way? 所以我的问题是,有没有更好的方法?
Searching the path of the Mongodb.exe file in Registry can be useful. 在注册表中搜索Mongodb.exe文件的路径可能很有用。 Try to find the exe path from the Registry entry.
尝试从注册表项中找到exe路径。
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\MongoDB\\MongoDB Connector"); RegistryKey键= Registry.LocalMachine.OpenSubKey(“ Software \\ MongoDB \\ MongoDB连接器”); if (key != null) { Object o = key.GetValue("InstallPath");
if(key!= null){对象o = key.GetValue(“ InstallPath”); }
}
Object o will give the path of MongoDB.exe file. 对象o将给出MongoDB.exe文件的路径。
PS: If your application does not have rights to access the local machine's registry, then this approach won't work PS:如果您的应用程序无权访问本地计算机的注册表,则此方法将不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.