【问题标题】:Mono Linux Shell commandMono Linux Shell 命令
【发布时间】:2015-06-30 10:03:07
【问题描述】:

我在 Linux 的 Mono 上执行了这个命令:

System.Diagnostics.Process.Start("snmpwalk", " -v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0 > /home/dana/Desktop/test.txt")

它应该在桌面上的 .txt 文件中获取 SystemUpTime,我在应用程序输出中得到了答案,但它没有创建文件。

是否可以使用此命令在 .txt 文件中获取答案?提前致谢。

【问题讨论】:

    标签: c# linux shell mono snmp


    【解决方案1】:

    我认为您无法通过这种方式获得输出重定向。

    但是你可以这样做:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    
    proc.Start("snmpwalk", " -v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0")
    
    //Then use the StandardOutput property of the process to read its output
    string procOutput = proc.StandardOutput.ReadToEnd();
    
    //Write the output to your file
    System.IO.File.WriteAllText("/home/dana/Desktop/test.txt", output);
    

    以上示例需要修改

    阅读 Dana 评论后,我扩展了上面的示例。

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    
    //this should tell not to start a console
    proc.StartInfo.UseShellExecute = False
    
    //this should tell that you are redirecting process output
    proc.StartInfo.RedirectStandardOutput = True
    
    proc.StartInfo.FileName = "snmpwalk"
    proc.StartInfo.Arguments = "-v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0"
    
    proc.Start()
    
    //Then use the StandardOutput property of the process to read its output
    string procOutput = proc.StandardOutput.ReadToEnd();
    
    //Write the output to your file
    System.IO.File.WriteAllText("/home/dana/Desktop/test.txt", output);
    

    我尝试将一个简单的 ping 输出重定向到一个文本文件,它成功了。

    【讨论】:

    • 我已经尝试过您的解决方案,但没有奏效。执行后应用程序关闭,但 .txt 文件未更改(空)。
    • 编辑:无法使用实例引用访问成员“System.Diagnostics.Process.Start(string, string)”;而是用类型名称来限定它。在网上搜了一下,发现是这样的:codeproject.com/Questions/75618/…...回到开头...
    • 感谢您最后的编辑。现在它完美地工作了。非常感谢!
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多