Visual Studio C# - Console.ReadLine()无法在exe文件中工作

xe55xuns  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(179)

我尝试将我的C#解决方案编译为.exe文件。我目前正在使用Idea Rider,并在/bin/...中执行了自动生成的输出。
然而,当我通过双击它来运行文件,或者在终端中手动执行它或通过.bat文件来运行文件时,它并没有像预期的那样提示输入。
我的代码:

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

问题出在main函数中-不知何故,这个Console.ReadLine()没有被执行:

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


也许我执行文件的方式不对,但我找不到其他方式。
提前感谢!
洛伦茨

4bbkushb

4bbkushb1#

问题出在我的.csproject档案中的输出类型。
那是:

<OutputType>WinExe</OutputType>

现在它是:

<OutputType>Exe</OutputType>

相关问题