简体   繁体   English

获取主可执行文件 C# Visual Studio 的相对路径

[英]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.

相关问题 Visual Studio:在C#程序中插入对可执行路径的引用 - Visual Studio: Inserting reference to executable path within C# program 在c#,visual studio 2013中使用数据库文件的相对路径 - Using a relative path for a database file in c#, visual studio 2013 在Visual Studio C#中处理可执行文件 - handling an executable file in Visual Studio C# Visual Studio .NET C#可执行文件跟踪 - Visual Studio .NET C# executable traces 使用 C# 获取可执行文件的绝对路径? - Getting the absolute path of the executable, using C#? 使用相对路径从C#解决方案运行外部可执行文件 - Running external executable from C# solution with relative path 使用相对路径使用C#在Visual Studio中打开Excel文件 - using relative path to open excel file in visual studio using c# Visual Studio c#相对路径,即使找到正确的路径,程序也会搜索两个路径 - Visual Studio c# relative path, program searching two paths even when it finds the correct one 使用MS Visual Studio 2010在C#中读取Excel文件:如何给出相对路径? - Reading Excel File in C# using MS Visual Studio 2010: How to give Relative Path? 在Visual Studio中C#文件中没有Main()是否正常? - Is it normal that there is no Main() in files of C# in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM