Reputation: 6505
I am building my first WPF application - a developer tool. I am building it in .Net Core 3.1. I have a requirement to use the .Net Core CLI to create another application from within the tool. I have really two questions:
Upvotes: 3
Views: 3356
Reputation: 22089
- Is this possible? I have done a lot of research but I have found nothing so far.
Of course, it is not even WPF related. You can run the dotnet
CLI just like any other program with Process
. The code below will call the help for dotnet
.
var process = new Process();
var startInfo = new ProcessStartInfo
{
FileName = "dotnet.exe",
Arguments = "-h"
};
process.StartInfo = startInfo;
process.Start();
If you do not want do display the console widow, you can add these options to the start info.
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Keep in mind that commands may fail and you should handle the console output to react on that. Look here for a related post that demonstrates how you can redirect console output.
- Is there a CLI command to add a new class to a class library project? The tool needs to create a new solution that compiles with all classes it creates added to the project.
Let's do a quick walkthrough to show the essential parts of your application. Since it is is always the same code for calling dotnet
but with different arguments, I will only show those.
Arguments = @"new classlib -o C:\The\Path\To\Your\Solution -n MyProject"
The classlib
command will create a class library in the given directory with the name MyProject
.
File.Create(@"C:\The\Path\To\Your\Solution\MyClass.cs");
File.WriteAllText(@"C:\The\Path\To\Your\Solution\MyOtherClass.cs", cSharpClassBoilerPlateCode);
As far as I know there is no built-in command to create single class files. However, you can just create a .cs
file with File.Create
or File.WriteAllText
that contains some boilerplate code directly in your project directory. The .NET SDK-style project format includes all source files in the project directory by default, unless you overrode this behavior.
Arguments = @"build C:\The\Path\To\Your\Solution\MyProject.csproj"
This is only a starter example. The dotnet
command line interface offers a lot more options to customize and work with solutions and projects, see below for reference.
Upvotes: 10
Reputation: 169270
Is this possible?
Sure. You can invoke any executable, including the dotnet
command-line tool, using the Process.Start
API:
//dotnet new wpf:
System.Diagnostics.Process.Start(@"C:\Program Files\dotnet\dotnet", "new wpf");
Is there a CLI command to add a new class to a class library project?
No, but you any file with a matching file extension that you add to the project folder will be considered when building the project so you don't really need to explicitly add a class to the project.
Upvotes: 1