首页 新闻 会员 周边

.Net Core 项目 打包成一个单独的可执行exe文件,无法打包bin/Debug/下的exe文件

0
悬赏园豆:5 [已解决问题] 解决于 2021-12-08 21:54

我的项目调用了一个exe文件,然后在打包发布成一个单独的可执行文件时,无法将调用的那个exe文件打包进来……

windows10.0.17763.0\publish\win-x64'. 系统找不到指定的文件。

这个exe,是个控制台参数文件,需要通过cmd进行使用,我把他封装成了个WinForm的东西,在打包的时候发现bin下面粘贴进去的这个exe无法进行打包

打包配置文件

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration>Debug</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Debug\net6.0-windows10.0.17763.0\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishReadyToRun>False</PublishReadyToRun>
    <PublishTrimmed>True</PublishTrimmed>
    <PublishSingleFile>True</PublishSingleFile>
  </PropertyGroup>
</Project>
echo_lovely的主页 echo_lovely | 小虾三级 | 园豆:1409
提问于:2021-12-08 17:07
< >
分享
最佳答案
0

试试在 .csproj 中包含一下这个 exe 文件

收获园豆:5
dudu | 高人七级 |园豆:30948 | 2021-12-08 17:26

包括进来了,还是不行……

我感觉:是路径写的不对

using System.Diagnostics;

namespace ClearPicture_Core.Utils
{
    public  class ProcessUtils
    {
        private const string exeName = "./realesrgan-ncnn-vulkan.exe";//这个是一个相对路径,调试的时候这么写,肯定是ok的……
        private const string exeParam = " -i \"{0}\" -o \"{1}\" -n {2}";

        /// <summary>
        /// 启动进程进行图片的处理
        /// </summary>
        /// <param name="inputFile">输入图片的路径</param>
        /// <param name="outPutFile">输出图片的路径</param>
        /// <returns>进程对象</returns>
        public static Process ProcessImage(string inputFile, string outPutFile, string param)
        {
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = Path.GetFullPath(exeName);
                process.StartInfo.CreateNoWindow = true;//不在新窗口中启动该进程。
                process.StartInfo.UseShellExecute = false;//不使用操作系统 shell 启动进程
                process.StartInfo.RedirectStandardError = true;// 将控制台输出重定向,可以通过进程对象来获取到黑窗口中的输出

                string parameter = string.Format(exeParam, inputFile, outPutFile, param);
                process.StartInfo.Arguments = parameter;
                process.Start();
                return process;
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText("./exception.txt", ex.ToString());
                System.IO.File.AppendAllText("./exception.txt", Path.GetFullPath(exeName));
                return null;
            }
        }
    }
}

异常文件

System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'E:\Visual_Studio\WinForm\ClearPicture_Core\bin\Debug\net6.0-windows10.0.17763.0\publish\win-x64\realesrgan-ncnn-vulkan.exe' with working directory 'E:\Visual_Studio\WinForm\ClearPicture_Core\bin\Debug\net6.0-windows10.0.17763.0\publish\win-x64'. 系统找不到指定的文件。
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at ClearPicture_Core.Utils.ProcessUtils.ProcessImage(String inputFile, String outPutFile, String param) in E:\Visual_Studio\WinForm\ClearPicture_Core\Utils\ProcessUtils.cs:line 28
E:\Visual_Studio\WinForm\ClearPicture_Core\bin\Debug\net6.0-windows10.0.17763.0\publish\win-x64\realesrgan-ncnn-vulkan.exe
echo_lovely | 园豆:1409 (小虾三级) | 2021-12-08 17:43

又或者无法包括类似的可执行文件,估计只能打包成安装的那种,安装了会解压一大堆东西,这样就有相对路径了

echo_lovely | 园豆:1409 (小虾三级) | 2021-12-08 17:49

@echo_lovely: 包含之后,build输出文件夹中是否有包含的文件?

dudu | 园豆:30948 (高人七级) | 2021-12-08 17:56

@dudu:

我勾选了这个 始终复制,
然后
发布的文件夹里多了bin目录,

然后这路径还是不对……
进不去!就是进不去!

echo_lovely | 园豆:1409 (小虾三级) | 2021-12-08 20:39

@dudu: 我好像听明白你说什么了,我去调整下结构

echo_lovely | 园豆:1409 (小虾三级) | 2021-12-08 21:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册