相关疑难解决方法(0)

在c#中使用ping

当我使用Windows Ping远程系统时,它说没有回复,但是当我用c#ping时,它表示成功.Windows正确,设备未连接.为什么我的代码能够在Windows不成功时成功ping?

这是我的代码:

Ping p1 = new Ping();
PingReply PR = p1.Send("192.168.2.18");
// check when the ping is not success
while (!PR.Status.ToString().Equals("Success"))
{
    Console.WriteLine(PR.Status.ToString());
    PR = p1.Send("192.168.2.18");
}
// check after the ping is n success
while (PR.Status.ToString().Equals("Success"))
{
    Console.WriteLine(PR.Status.ToString());
    PR = p1.Send("192.168.2.18");
}
Run Code Online (Sandbox Code Playgroud)

c# ping

83
推荐指数
2
解决办法
17万
查看次数

如何在C#中异步读取结束进程输出?

我在C#中异步读取一个进程的输出时遇到问题.我在这个网站上发现了一些其他类似的问题,但它们对我没有帮助.这是我做的:

  1. 制作新流程
  2. 设置startinfo -FileName,Arguments,CreateNoWindow(true),UseShellExecute(false),RedirectStandardOutput(true)
  3. 将事件处理程序添加到OutputDataReceived;
  4. 启动进程,BeginOutputReadLine,然后是WaitForExit().

它工作正常,但启动过程的输出写了一些%我想得到的百分比(),但我不能,因为我的代码逐行读取,百分比没有显示.

例:

%0,%1...%100
Finished.
Run Code Online (Sandbox Code Playgroud)

我的输出:

%0
Finished. 
Run Code Online (Sandbox Code Playgroud)

这是我的程序的当前代码:

StringBuilder sBuilder = new StringBuilder();
static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    sBuilder.AppendLine(e.Data);
}

static void CommandExecutor()
{
    Process process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = /*path of the program*/,
            Arguments = /*arguments*/,
            CreateNoWindow = true,
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden,
            RedirectStandardOutput = true
        }
    };

    process.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);

    process.Start();

    process.BeginOutputReadLine();

    process.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous process

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

标签 统计

c# ×2

asynchronous ×1

ping ×1

process ×1