douping5015 2016-03-28 04:46
浏览 49
已采纳

如何执行命令并获取其输出

I've been learning a few things about ASP for the past few days. I wanted to convert this PHP code line into ASP but I'm kinda stuck with it:

$online = exec('netstat -a -n |find "5816" |find "ESTABLISHED" /c') +1;

I have tried creating a variable to store the data but unable to figure out how to check the port 5816 and count the amount of connections. Help is appreciated!

It should be basically a command to be run in cmd to check the port and no. of connections established by it !

  • 写回答

1条回答 默认 最新

  • dqalnwuci494308 2016-03-28 05:36
    关注

    Executing a command getting its output

    You can use this code to execute the above command:

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo = new System.Diagnostics.ProcessStartInfo()
    {
        UseShellExecute = false,
        CreateNoWindow = true,
        WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
        FileName = "cmd.exe",
        Arguments = "/C netstat -a -n |find \"5816\" |find \"ESTABLISHED\" /c",
        RedirectStandardError = true,
        RedirectStandardOutput = true
    };
    process.Start();
    // Now read the value, parse to int and add 1 (from the original script)
    int online = int.Parse(process.StandardOutput.ReadToEnd()) + 1;
    process.WaitForExit();
    

    This code starts the cmd.exe executable. Using the /C argument, you can give it the command you want to execute

    A simple search in Stackoverflow gave me hundreds of questions that could help you.

    Source: How To: Execute command line in C#, get STD OUT results, Run Command Prompt Commands

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料