-1

I have to make a WPF apllication which adding a task to Task scheduler. So I decided to write a program which compiling cs file and add its exe to Task scheduler (I tried Microsoft.CodeAnalysis.CSharp.Scripting and Microsoft.CodeAnalysis.Scripting but I do not know how to achieve exe file from them). Unfortunately I have a little problem with that. Compiling of cs file ends successfully but running exe file ends with an error:

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

MainProgram.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;

TaskProgram.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();
}

Little story of compiling line. At the beginning I added only one reference to compiling line "Microsoft.Data.SqlClient". But it is not enough and compiling ends with errors like that

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

and

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

So I added missing abc123 over and over until compiling ends succesfully.

csc command (in code) to compile:

@"/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 command (command line) to compile:

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"

Any help will be appriaciated.

8
  • Are you getting a different result from running the compiler from Process.Start vs from a command prompt? How about ordinary command prompt vs "Developer Tools" command prompt created by Visual Studio installer? Commented Dec 6, 2024 at 14:59
  • In all cases there is the same error results about System. Runtime. Commented Dec 6, 2024 at 16:40
  • The following may be of interest: stackoverflow.com/a/78271636/10024425 and stackoverflow.com/questions/75490394/… Commented Dec 6, 2024 at 18:05
  • Is there any particular reason why you don't want to create a csproj file, place it next to the cs one and just run dotnet build? Commented Dec 6, 2024 at 19:32
  • 1
    @GuruStron dotnet build is a command which I need. Thank you :) Commented Dec 11, 2024 at 9:52

1 Answer 1

0

Thank to Guru Stron. dotnet build is a command which I need. I have created csproj file in a dir where there is my cs.file. I named this dir TestScript.

TestScript.csproj file

<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>

Program.cs file

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\TestScript.exe");

PS: Why minus 1 to my question?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.