[英]Getting the relative path to main executable C# Visual Studio
I am trying to do what I thought was a relatively easy task to do, at runtime I would like to obtain the path of the executable to be able to use this directory to create a subdirectory for storing my settings xml file我正在尝试做我认为相对容易做的事情,在运行时我想获取可执行文件的路径,以便能够使用该目录创建一个子目录来存储我的设置 xml 文件
public ProgramSettings()
{
filePath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\Settings\";
}
I build the debug build, this run fine - but I was expecting that if I move the executable to another location (or computer for that matter) it should pickup the new location.我构建了调试版本,运行良好 - 但我期待如果我将可执行文件移动到另一个位置(或就此而言的计算机),它应该会拾取新位置。 This code still returns the original path of the Visual Studio environment even on another machine.
即使在另一台机器上,此代码仍会返回 Visual Studio 环境的原始路径。 Can someone please explain what is going on???
有人可以解释发生了什么吗???
This should work:这应该有效:
public ProgramSettings()
{
var baseDir = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(ProgramSettings)).Location);
string fullPath = $"{baseDir}\\Settings\\";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.