使用 csc 命令执行已编译的 cs 文件失败,并出现错误“无法加载文件或程序集 System.Runtime 或其依赖项之一”

问题描述 投票:0回答:1

我必须创建一个 WPF 应用程序,将任务添加到任务计划程序。所以我决定编写一个程序,编译cs文件并将其exe添加到任务调度程序(我尝试了Microsoft.CodeAnalysis.CSharp.Scripting和Microsoft.CodeAnalysis.Scripting,但我不知道如何从中实现exe文件)。不幸的是我对此有一点问题。 cs文件编译成功结束,但运行exe文件结束时出现错误:

Could not load file or assembly System.Runtime or one of its dependencies

主程序.cs

Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\Roslyn\csc.exe"; //new compiler
process.StartInfo.Arguments = @"/out:""TestScript.exe"" /reference:""Microsoft.Data.SqlClient.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Data.Common.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.ComponentModel.Primitives.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Runtime.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\mscorlib.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Console.dll"" ""TestScript.cs""";
process.Start();
process.WaitForExit();
string PathToCSFIle = @"TestScript.exe";
Process.Start(PathToCSFIle);
return 0;

任务程序.cs

using System;
using Microsoft.Data.SqlClient;
var ConnectionString = @"Data Source=MyDataSource;Database=MyDatabase;Integrated Security=True;Encrypt=True;Trust Server Certificate=True;MultipleActiveResultSets=true";
var SQLQuery = @"SELECT TOP(100) * FROM MyTable";
using (var connection = new SqlConnection(ConnectionString))
{
    connection.Open();
    using (var command = new SqlCommand(SQLQuery, connection))
    {
        using (var reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var ColumnData = reader["MyColumn"].ToString()!;
                Console.WriteLine(ColumnData);
            }
        }
    }
    connection.Close();
}

编译行的小故事。一开始我只添加了一个对编译行“Microsoft.Data.SqlClient”的引用。但这还不够,编译结束时会出现这样的错误

The type 'XXX' is defined in an assembly that is not referenced. You must add a reference to assembly 'abc123'

'T': type used in a using statement must be implicitly convertible to 'System.IDisposable'

所以我一遍又一遍地添加缺少的abc123,直到编译成功结束。

csc 命令(代码中)进行编译:

@"/out:""TestScript.exe"" /reference:""Microsoft.Data.SqlClient.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Data.Common.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.ComponentModel.Primitives.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Runtime.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\mscorlib.dll"" /reference:""C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Console.dll"" ""TestScript.cs""";

csc命令(命令行)进行编译:

csc /out:"TestScript.exe" /reference:"Microsoft.Data.SqlClient.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Data.Common.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.ComponentModel.Primitives.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Runtime.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\mscorlib.dll" /reference:"C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0\System.Console.dll" "TestScript.cs"

如有任何帮助,我们将不胜感激。

c# command-line runtime windows-task-scheduler csc
1个回答
0
投票

感谢斯特朗大师。 dotnet build 是我需要的命令。我已经在我的 cs.file 目录中创建了 csproj 文件。我将此目录命名为 TestScript。

TestScript.csproj 文件

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
  </ItemGroup>

</Project>

程序.cs文件

Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = @"C:\Program Files\dotnet\dotnet.exe";
process.StartInfo.Arguments = @"build TestScript\TestScript.csproj -t:Build";
process.Start();
process.WaitForExit();
Process.Start(@"TestScript\bin\Debug\net9.0");
© www.soinside.com 2019 - 2024. All rights reserved.