I tried to compile my C# Solution to a .exe file. I am currently working with Idea Rider and I executed the automatically generated Output in /bin/... .
However when I run the file by double-clicking it, or by executing it manually in the terminal or by .bat file it doesn't prompt for an input as expected.
My code:
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using Renci.SshNet;
namespace VLAN_Switcher
{
class DeploymentMode
{
public static void Main(string[] args)
{
string laboratory = "";
while (!laboratory.Equals("76") && !laboratory.Equals("61") && !laboratory.Equals("101"))
{
Console.WriteLine("In welches VLAN soll der LehrerPC gegeben werden(61,76 oder 101)?:");
laboratory = Console.ReadLine();
}
string localIP = FindIp();
ChangePortToNewLaboratory(localIP, laboratory);
ChangeIP(localIP, laboratory);
}
public static string FindIp()
{
string localIP;
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
localIP = endPoint.Address.ToString();
return localIP;
}
}
public static void ChangePortToNewLaboratory(string localIP, string laboratory)
{
string currentVLAN = localIP.Split(".")[2];
//Connection information
string host = $"";
int port = 22;
string username = "";
string password = "";
using (var client = new SshClient(host, port, username, password))
{
client.Connect();
ShellStream shellStream = client.CreateShellStream(string.Empty, 0, 0, 0, 0, 0);
shellStream.Write("en\n");
shellStream.Write("conf t\n");
shellStream.Write("interface GigabitEthernet1/0/48\n");
shellStream.Write($"switchport access vlan {laboratory}\n");
shellStream.Close();
client.Disconnect();
}
}
public static void ChangeIP(string localIP, string laboratory)
{
NetworkInterface interfaceLan = null;
string[] ipAddressArr = localIP.Split(".");
ipAddressArr[2] = laboratory;
string newIpAddress = String.Join(".",ipAddressArr);
ipAddressArr[3] = "254";
string newGateway = String.Join(".",ipAddressArr);
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (ip.Address.ToString() == localIP)
{
interfaceLan = ni;
}
}
}
}
var process = new Process
{
StartInfo = new ProcessStartInfo("netsh", $"interface ip set address \"{interfaceLan}\" static {newIpAddress} 255.255.255.0" + (string.IsNullOrWhiteSpace(newGateway) ? "" : $"{newGateway} 1")) { Verb = "runas" }
};
process.Start();
process.Dispose();
}
}
}
The problem is in the main function - somehow this Console.ReadLine() doesn't get excecuted:
public static void Main(string[] args)
{
string laboratory = "";
while (!laboratory.Equals("76") && !laboratory.Equals("61") && !laboratory.Equals("101"))
{
Console.WriteLine("In welches VLAN soll der LehrerPC gegeben werden(61,76 oder 101)?:");
laboratory = Console.ReadLine();
}
string localIP = FindIp();
ChangePortToNewLaboratory(localIP, laboratory);
ChangeIP(localIP, laboratory);
}
Project File:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>VLAN_Switcher</RootNamespace>
<ApplicationIcon>Herunterladen.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SSH.NET" Version="2020.0.2" />
</ItemGroup>
</Project>
Maybe I am executing the file in a wrong way, but I can't find any other way.
Thanks in advance!
Lorenz
The problem was the output type in my .csproject file.
It was:
<OutputType>WinExe</OutputType>
Now it is:
<OutputType>Exe</OutputType>