捕获它用于运行程序的目录路径
本文关键字:路径 程序 运行 用于 | 更新日期: 2023-09-27 17:55:54
我有一个 C# 应用程序,它假设它从 bin 目录运行
string current_directory = Directory.GetCurrentDirectory(); //get current directory, this is the bin dir
string parent_dir = Directory.GetParent(current_directory).ToString();// this is parent of bin dir
string _Config1 = parent_dir + "''config''x.cfg";
string _Config2 = parent_dir + "''config''y.cfg";
string _Log = parent_dir + "''log''z.log";
问题是由于某些原因,用户无法转到 bin 目录并运行应用程序(只需键入"application_name")。他必须使用路径(即d:'blah'1'blah'2'blah'3'bin'application_name
)运行它
当他这样做时,他得到
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().洛阳离子)
因此,要么我必须捕获他用于运行程序的路径并在我的程序中使用它,要么以某种方式使我的应用程序能够使用路径运行。
您可以使用
AppDomain.CurrentDomain.BaseDirectory
.
将Application.StartupPath
用于WinForms
。
您可以使用反射:
var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().GetCodebase );