astral
astral
CC#
Created by astral on 9/20/2024 in #help
How to Build Multiple Binaries from Dotnet New Console?
I have the following:
$ dotnet new console
$ dotnet run
Hello, world!
$ dotnet new console
$ dotnet run
Hello, world!
I move Program.cs to src/Program.cs and it still seems to work. I replace src/Program.cs with src/echo.cs with the following:
using System;

class Echo {
public static void Main(string[] args) {
foreach (string arg in args) {
Console.Write(arg);
// Put space if not last argument
if (arg != args[args.Length - 1]) {
Console.Write(" ");
}
Console.WriteLine();
}
}
};
using System;

class Echo {
public static void Main(string[] args) {
foreach (string arg in args) {
Console.Write(arg);
// Put space if not last argument
if (arg != args[args.Length - 1]) {
Console.Write(" ");
}
Console.WriteLine();
}
}
};
This is note-box.csproj:
<Project Sdk="Microsoft.NET.Sdk">

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

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

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

</Project>
I see there's the executable note-box and it behaves like echo when I pass args. When I add a second main entry point, src/true.cs:
class True {
public static void Main() {
System.exit(0);
}
};
class True {
public static void Main() {
System.exit(0);
}
};
It errors. The goal is to have echo and true be two different programs. Another solution that I'm not sure if will work is if I just have one entry point: src/note-box.cs:
using System;

class NoteBox {
public static void Main(string[] args) {
switch (args[0]) {
case "echo":
// Call echo
break;
case "true":
// Call true
break;
}
}
};
using System;

class NoteBox {
public static void Main(string[] args) {
switch (args[0]) {
case "echo":
// Call echo
break;
case "true":
// Call true
break;
}
}
};
This would do something like ./bin/note-box echo or ./bin/note-box true. If this approach is better, I would want to make symlinks to note-box and have the name of the link be what it calls. In C it's something like:
char *cmd = strrchr(argv[0], '/');
cmd = cmd ? cmd + 1 : argv[0];
char *cmd = strrchr(argv[0], '/');
cmd = cmd ? cmd + 1 : argv[0];
I'm not sure how to resolve link name of executable in a C# way.
39 replies