相关疑难解决方法(0)

是否可以在C#程序中使用多个main()方法?

是否可以main()在C#控制台应用程序中使用多个方法(具有相同的参数)?如果是这样,怎么样?

c#

13
推荐指数
1
解决办法
2万
查看次数

为WinForm应用程序分配控制台

我使用以下代码为WinForm应用程序分配控制台.控制台窗口成功显示,输出就在那里.但是当我关闭控制台窗口时,我的WinForm应用程序同时关闭.为什么?我想保留WinForm窗口.

private void btn_to_console_Click(object sender, EventArgs e)
{
    if (NativeMethods.AllocConsole())
    {
        lbl_console_alloc_result.Text = "Console allocation successfully!";
        IntPtr stdHandle = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
        Console.WriteLine("from WinForm to Console!");
        lbl_console_alloc_result.Text = Console.ReadLine();
    }
    else
        lbl_console_alloc_result.Text = "Console allocation failed!";
}

[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GetStdHandle")]
public static extern System.IntPtr GetStdHandle(Int32 nStdHandle);

/// Return Type: BOOL->int
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "AllocConsole")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool AllocConsole();
Run Code Online (Sandbox Code Playgroud)

提前致谢...

c# operating-system

7
推荐指数
1
解决办法
1979
查看次数

Visual Studio代码:程序有多个入口点定义?

在将其标记为副本之前,请先阅读说明.

使用visual studio代码创建了C#项目.这个项目包含两个.cs文件Addition.cs和Substraction.cs这两个文件都包含main()函数,这两个文件包含两个不同的程序.

Addition.cs文件中的代码

using System;

namespace Example
{
    class Addition
    {
        static void Main(string[] args)
        {
            int sum = 3 + 2;
            Console.WriteLine(sum);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Substraction.cs文件中的代码

using System;

namespace Example
{
    class Substraction
    {
        static void Main(string[] args)
        {
            int sub = 3 - 2;
            Console.WriteLine(sub);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想逐个测试这个程序,但是当我这样做的时候

"dotnet run"

它失败并出现上述错误.

我知道因为同一项目中的两个main()函数(入口点)创建了这个错误,但是这可以通过设置启动项目在visual studio中克服.

我正在使用Visual Studio代码,我无法设置启动项目.有没有办法在visual studio代码中设置c#项目的入口点?

c# csproj visual-studio visual-studio-code

4
推荐指数
1
解决办法
3769
查看次数