c# - 已发布的 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[] 对象,我不确定如何从这里提取相对文件路径。
解决方案
string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
此代码提供的文件路径根据构建模式而变化: 1. 在调试模式下,它是.../bin/Debug。2. 在发布模式下是.../bin/Release。
因此,请确保将 .exe 文件复制到相应的文件夹中。
推荐阅读
- java - 更新 zip 存档中的文件
- java - 如何使@SpringBootTest 使用可配置的属性
- python - Python Pandas条件计算逐行
- android-studio - 在单元测试中处理 protobuf 类时出现 NoSuchMethod 错误
- android - 在我的 Android 设备上收集热数据的最佳方式是什么?
- ios - UITableViewCell 在实现为 UITableView 的 FooterView 时无法识别按钮的操作,具有多个页脚视图
- amazon-web-services - AWS 证书不适用于 www.example.com
- google-sheets - 从多个电子表格中计算唯一性和分组依据
- python - 使用 mypy 的“不支持的索引分配目标”,具体取决于与分配有关的类型提示时刻
- typescript - 将 wasm 与汇总捆绑时“缺少导出”