c# -程序集名称字符串在加载Exe时呈现不正确



这里有一个GIF显示错误

它可以很好地与DownloadFile()一起工作,它在正确的名称和路径下下载(甚至在它正确之前的mbox) -但是在它之后的所有内容都不能是正确的名称和路径。

需要。exe来测试?
https://github.com/ImReallyShiny/SAIO/blob/master/Test.exe
(如果需要扫描,但它是100%安全的)


这绝对是魔法。

当我从Visual Studio上的"开始"按钮运行我的"自动更新"代码时,它输出正确的文件路径和文件名(带有。exe)…
但是当我打开build .exe时,它会做一些魔法,导致它在(&包括"-",但以某种方式保留。exe。

代码是完全相同的。exe的唯一一件事我可以看到的原因,是一些问题与Webclient获得版本超过1次(但它绑定到一个字符串变量,所以它不应该重要)或某种故障与我的VisualStudio设置(缓存,传播,PC重启需要或东西)


这是一个很难解释的问题,所以我添加了一些内联注释来解释问题在哪里以及它是什么。

代码:

//Using WebClient, get the Newest File Version;
using (System.Net.WebClient wc = new System.Net.WebClient())
{
    //Latest Version; (This is the number after the - for the filename - Excluding .exe)
    string LatestVersion = wc.DownloadString("https://raw.githubusercontent.com/ImReallyShiny/SAIO/master/version.txt");
    //This is the Location itself e.g C:/Users/Shiny/Desktop/{appname}.exe
    string ExecutableLocation = typeof(Program).Assembly.CodeBase.Replace("file:///", "");
    //The build's File Version;
    string CurrentVersion = FileVersionInfo.GetVersionInfo(ExecutableLocation).ProductVersion;
    //Final Name String; As you can see it SHOULD output as: AppName-1.2.4.5.exe
    string CurrentExecutableName = typeof(Program).Assembly.GetName().Name + "-" + LatestVersion + ".exe";
    //If the Latest Version is Newer then the Current Version;
    if (LatestVersion != CurrentVersion)
    {
        //Download the Latest Version of the EXE file; (Gets the name and path perfectly fine)
        wc.DownloadFile("https://github.com/ImReallyShiny/SAIO/raw/master/SAIO.exe", CurrentExecutableName);
        //Show a MessageBox asking to open Explorer to the file; - This should output "{Path}/AppName-1.2.4.5.exe" but it only does when opening from VS
        DialogResult mb = MessageBox.Show("Continue usage on the new update. Open Explorer and go to the Directory containing the updated .exe located at: " + ExecutableLocation.Replace("SAIO.EXE", CurrentExecutableName + " ?""), "New Update Downloaded!", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        if (mb == DialogResult.Yes)
        {
            //Go to where SAIO is and select the New Update.exe; (This also fails to select the right .exe, It selects "SAIO.EXE" when it should be selecing the AppName-1.2.4.5.exe
            Process.Start("explorer.exe", "/select,"" + ExecutableLocation.Replace("/", "\").Replace("SAIO.EXE", CurrentExecutableName) + """);
        }
    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}
明白我在说什么吗?

这只是文件名(有时)是SAIO.exe或其他情况。

看到Replace没有过载允许您指定InvariantCultureIgnoreCase(或somethingElseIgnoreCase),最好的解决方案是使用Path.GetDirectoryName来获得您想要的完整文件夹名称,而不是仅仅期望在路径中只有一个SAIO.EXE

。您的最后一行可以是(未经测试):

Process.Start("explorer.exe", "/select,"" +
    Path.GetDirectoryName(ExecutableLocation.Replace("/", "\"))
    + "\" + CurrentExecutableName + """);

GetDirectoryName可以很好地处理/分隔符,但是您仍然需要在某个时候调整它们以确保explorer调用正常工作。

最后一行应该是(同样未经测试):

Process.Start("explorer.exe", "/select,"" +
    Path.Combine(Path.GetDirectoryName(ExecutableLocation.Replace("/", "\")),
        CurrentExecutableName) + """);

最新更新