C# 添加要运行的程序不会添加完整路径

本文关键字:添加 路径 程序 运行 | 更新日期: 2023-09-27 18:30:36

我添加到注册表运行的方式有些奇怪。

当我使用

 private static string AppPath = new Uri((System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).LocalPath;

要在运行注册表中设置路径,它工作正常,但是例如,如果文件夹名称为"C#",则添加的键将在#之前删除所以应该是:

c

:/desktop/c#/myprogram.exe但它是

c

:/桌面/c

你们有什么问题可以帮忙吗?

C# 添加要运行的程序不会添加完整路径

我认为 Uri 转义符号存在问题。试试这个:

string AppPath = System.Reflection.Assembly.GetExecutingAssembly ().Location;

我无法复制你所看到的。我想也许你错过了一些信息:

var uri = new Uri("c:/desktop/c#/myprogram.exe");
string raw = uri.ToString(); // "file:///c:/desktop/c%23/myprogram.exe"
string local = uri.LocalPath; // "c:'desktop'c#'myprogram.exe"

您确定那里的代码库属性会有什么结果吗?

发生这种情况是因为#字符在Uri中编码并变为%23

我不确定为什么要使用 Uri 来获取可执行文件的位置。有一个更好的方法(正如夜蛇发布的那样)。

但是,如果必须使用Uri(无论出于何种原因),则可以通过执行以下操作来获取完整路径:

        string s = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
        Uri u = new Uri(s);
        string local = u2.LocalPath+u2.Fragment.Replace('/','''');