首页 > 解决方案 > 已发布的 C# 控制台应用程序 - 系统找不到指定的文件

问题描述

我正在使用一个非常短的 C# 控制台应用程序来启动用 python 编写的可执行文件,这样我就可以利用单击一次部署。

在调试模式下,我能够找到作为资源包含的两个可执行文件。但是,一旦应用程序发布,我似乎无法找到它们的相对文件路径。我得到一个异常:“系统找不到指定的文件”

using System;
using System.Diagnostics;


namespace UpdateConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
            string driver_path = string.Format("{0}Resources\\chromedriver.exe", System.IO.Path.GetFullPath(System.IO.Path.Combine(RunningPath, @"")));
            string exe_path  = string.Format("{0}Resources\\DUU.exe", System.IO.Path.GetFullPath(System.IO.Path.Combine(RunningPath, @"")));

            Process.Start(exe_path,driver_path);
        }
    }
}

我也尝试过像这样引用资源:

Properties.Resources.DUU;

但这会返回一个 byte[] 对象,我不确定如何从这里提取相对文件路径。

标签: c#.netconsole-applicationclickonce

解决方案


 string RunningPath = AppDomain.CurrentDomain.BaseDirectory;

此代码提供的文件路径根据构建模式而变化: 1. 在调试模式下,它是.../bin/Debug。2. 在发布模式下是.../bin/Release。

因此,请确保将 .exe 文件复制到相应的文件夹中。


推荐阅读