加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – .Net核心命令行应用程序| Process.Start()在某些机器上运

发布时间:2020-12-15 22:45:08 所属栏目:百科 来源:网络整理
导读:背景 我正在编写.NET核心命令行应用程序作为我的构建系统的CLI.构建系统的这一部分涉及从类库生成NuGet包.我正在使用ProcessStartInfo.cs和Process.cs来调用nuget.exe来发出pack命令(nuget.exe位置在系统PATH中). 注意:我不能使用dotnet CLI进行打包,因为类
背景

我正在编写.NET核心命令行应用程序作为我的构建系统的CLI.构建系统的这一部分涉及从类库生成NuGet包.我正在使用ProcessStartInfo.cs和Process.cs来调用nuget.exe来发出pack命令(nuget.exe位置在系统PATH中).

>注意:我不能使用dotnet CLI进行打包,因为类库不是.NET Core项目,所以请不要说“为什么不使用dotnet pack.

> C#.NET Core(我的CLI)
> C#.NET 4.5(类库)
> Thoughtworks GoCD(构建服务器)
> Windows Server 2016(构建服务器操作系统)
> Windows 10(本地计算机操作系统)

问题

我面临的问题是,在我的个人计算机上,当我运行构建系统CLI时,一切都运行得很好;但是,当我的构建服务器运行它时,该进程似乎尝试启动,但随后退出而不抛出异常或返回任何类型的退出代码.两个帐户(我本地计算机上的我和构建服务器运行的帐户)都是本地管理员帐户.

Cmd.cs(执行过程)

public static class Cmd
{
    public static int Execute(string filename,string arguments)
    {
        var startInfo = new ProcessStartInfo
        {
            CreateNoWindow = true,FileName = filename,Arguments = arguments,};

        using (var process = new Process { StartInfo = startInfo })
        {
            try
            {
                process.Start();
                process.WaitForExit(30000);
                return process.ExitCode;
            }

            catch (Exception exception)
            {
                if (!process.HasExited)
                {
                    process.Kill();
                }

                Console.WriteLine($"Cmd could not execute command {filename} {arguments}:n{exception.Message}");
                return (int)ExitCode.Exception;
            }
        }
    }
}

Package.cs(使用Cmd.cs)

// Some code before this

// The below three variables are generated/provided and are made up here for show
var version = "1.0.0";
var package = $"MyLib.{version}";
var projectPath = "C:SomeMadeUpPath";
///////////

// Real code
var tempPath = $"{Path.Combine(Path.GetTempPath(),"NugetPackages")}";
var packagePath = Path.Combine(tempPath,package);

if (!Directory.Exists(tempPath))
{
    try
    {
        Directory.CreateDirectory(tempPath);
    }

    catch (Exception exception)
    {
        Console.WriteLine($"Could not create directory in user temp file: {exception.Message}");
        return (int) ExitCode.FilePermission;
    }
}

var filename = "nuget.exe";
var arguments = $"pack -Version {version} -OutputDirectory {tempPath} -properties Configuration=Release {projectPath}";
var exitCode = Cmd.Execute(filename,arguments);

if (exitCode != 0)
{
    Console.WriteLine($"Process failed to execute and exited with exit code: {exitCode}");
    return exitCode;
}

Console.WriteLine($"{package} built successfully");

// Some code after this

解决方法

我修改了我的代码以显示所有窗口输出,并且看起来相同版本的NuGet.exe在Windows 10 Pro与Windows Server 2016上的行为不同…不确定原因,但在Windows 10 Pro上我可以使用-Version标记来替换.nuspec中的$version $标记,但是在Windows Server 2016中,我无法执行此操作并且必须使用不同的标记(例如$package $)并使用-properties开关将$package $替换为我的版本.跛脚酱!

所以,而不是:

nuget.exe pack -Version 1.0.0 -OutputDirectory C:Somewhere -properties Configuration=Release MyLib

我必须使用

nuget.exe pack -OutputDirectory C:Somwhere -properties "Configuration=Release;package=1.0.0" MyLib

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读