Reputation: 53
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.
Upvotes: -1
Views: 95
Reputation: 53
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?
Upvotes: 0