相关疑难解决方法(0)

如何阻止C#控制台应用程序自动关闭?

Visual Studio上的我的控制台应用程序正在自动关闭,所以我想使用像C这样的东西system("PAUSE")在执行结束时"暂停"应用程序,我该如何实现?

c# console-application

354
推荐指数
9
解决办法
51万
查看次数

在C#中的控制台应用程序异步?

我有这个简单的代码:

public static async Task<int> SumTwoOperationsAsync()
{
    var firstTask = GetOperationOneAsync();
    var secondTask = GetOperationTwoAsync();
    return await firstTask + await secondTask;
}


private async Task<int> GetOperationOneAsync()
{
    await Task.Delay(500); // Just to simulate an operation taking time
    return 10;
}

private async Task<int> GetOperationTwoAsync()
{
    await Task.Delay(100); // Just to simulate an operation taking time
    return 5;
}
Run Code Online (Sandbox Code Playgroud)

大.这个编译.

但是让我们说我有一个控制台应用程序,我想运行上面的代码(调用SumTwoOperationsAsync())

 static  void Main(string[] args)
        {
             SumTwoOperationsAsync();
        }
Run Code Online (Sandbox Code Playgroud)

但我读过,(使用时sync),我不得不一路同步向上向下 :

问题:这是否意味着我的Main功能应该标记为 …

c# async-await c#-5.0 .net-4.5

168
推荐指数
2
解决办法
10万
查看次数

如何使用ILoggerFactory并登录到控制台

我在 .Net Core 3.1 中创建了一个简单的控制台应用程序。我想创建一个记录器并将其打印到控制台。

我有以下代码

class Program
{
    public static void Main(string[] args)
    {
        var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
        var logger = loggerFactory.CreateLogger(nameof(Program));
        logger.LogInformation("some logging info");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行命令dotnet run时,没有打印任何内容。

我还需要配置其他东西吗?

c# logging .net-core loggerfactory

5
推荐指数
0
解决办法
3168
查看次数

使用Process.Start()通过外部程序启动代码时进行调试

假设我有一个C#WinForms应用程序,它只是通过使用外部程序启动Process.Start(MyModule.exe).

我试图通过使用我的项目属性调试我的代码- > 调试 - > 启动操作 - > 启动外部程序(当然设置正确的工作目录).

我的另一个尝试是Debug - > Attach to process

更新:我的模块和外部程序都使用一个资源.所以外部程序释放它,然后调用Process.Start(),等待我的进程退出ans然后再次捕获资源.所以当你的程序已经运行时我无法附加.

它们都失败了 - Program.cs中的断点从未被击中.

那么,在这种情况下如何调试我的代码呢?

c# debugging external-process process.start

3
推荐指数
1
解决办法
2660
查看次数

C#简单的"Hello World"在控制台中缺少一行代码?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        string userName = "";
        int userAge = 0;
        int currentYear = 0;

        Console.Write("Please enter your name: ");
            userName = Console.ReadLine();
        Console.Write("Please enter your age: ");
        userAge = Convert.ToInt32(Console.ReadLine());
        Console.Write("Please enter the current year: ");
        currentYear = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Hello World, my name is {0} and I'm {1} years old, I was born on the year {2} .", userName, userAge, currentYear …
Run Code Online (Sandbox Code Playgroud)

c# visual-studio

3
推荐指数
1
解决办法
223
查看次数

运行后C#阻止CMD自动关闭?

我想阻止CMD在运行程序后自动关闭.

码:

static void Main(string[] args)
    {

        FileStream textfsw = new FileStream("fileone.txt", FileMode.Append, FileAccess.Write); // CREATING FILE
        StreamWriter textsw = new StreamWriter(textfsw);

        Console.Write(" Enter your ID: ");
        int ID = int.Parse(Console.ReadLine());
        Console.Write(" Enter your Name: ");
        string Name = Console.ReadLine();
        Console.WriteLine(" Thank you! your logged in as" +Name+ +ID);

        textsw.Close();
        textfsw.Close();

        FileStream textfs = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read); // READING A FILE USING ReadAllLines
        StreamReader textsr = new StreamReader(textfs);

        String[] lines = File.ReadAllLines("fileone.txt");
    }
Run Code Online (Sandbox Code Playgroud)

截图: 运行截图后

我希望在运行后显示以下内容并阻止cmd关闭:

Thank you! your logged …
Run Code Online (Sandbox Code Playgroud)

c# console console-application filestream

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

控制台窗口刚关闭?C#

Soooo这个程序似乎是合乎逻辑的,而且我已经能够在c ++和java中创建具有相同想法的程序,每当我运行它时,一切都会突然关闭......也许我只是下载了错误的东西

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            /* 
            int[] numbahs= new int[5];
            numbahs[0] = 4;
            numbahs[1] = 3;
            Console.WriteLine(numbahs[1].ToString());

            string[] cow = new string[] { "pee", "poop" };
            foreach (string pee in cow)
            {

                Console.WriteLine(pee);
            }
            */

            int x;
            int y;
            Console.WriteLine("enter x:");
            x = Console.Read();
            Console.WriteLine("enter y:");
            y = Console.Read();
            int z = (x > y)? 8 : 2;
            Console.WriteLine("x:{0} y:{1} z:{2}",x ,y, …
Run Code Online (Sandbox Code Playgroud)

c#

-1
推荐指数
1
解决办法
101
查看次数