繁体   English   中英

在C#中读取txt文件

[英]Read a txt file in C#

我在文件后面的代码中有一个.aspx页面,其中包含一些C#代码。

我想在命令提示符下执行命令,并将输出保存到.txt文件。 这将起作用,并创建一个新的.txt文件。

然后,我想从.txt文件中读取第一行,并将字符串存储在变量中。

我的当前代码在下面,但是,它引发“无法读取文件”。 我的代码中的错误消息。 为什么会这样,我该如何解决?

更新 :因此,我从原始帖子中修改了我的代码,并在从中读取之前检查了“ archivo_resultado”是否存在。 但是,debugview输出

"archivo_resultado doesn't exist."

因此,当然尝试读取它会引发错误。

在命令提示符下执行以下命令的最有效方法是:

'ejecutable_CheckMac + " " + archivo_temporal'

并将输出字符串存储在变量中并存储到文件(archivo_resultado)中?

代码

        string ejecutable_CheckMac = "C:\\inetpub\\wwwroot\\cgi-bin\\tbk_check_mac.exe";
        var archivo_temporal = "C:\inetpub\wwwroot\cgi-bin\log\DatosParaCheckMac_100942.txt";
        var archivo_resultado = "C:\inetpub\wwwroot\cgi-bin\log\ResultadoCheckMac_100942.txt";

        System.Diagnostics.Debugger.Log(0, null, "Declare cmd variable.");
        string cmd = ejecutable_CheckMac + " " + archivo_temporal + " > " + archivo_resultado;
        System.Diagnostics.Debugger.Log(0, null, "cmd: " + cmd);

        System.Diagnostics.Debugger.Log(0, null, "Start cmd execution.");
        System.Diagnostics.Process process = new System.Diagnostics.Process();

        var startInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd);
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;

        System.Diagnostics.Process.Start(startInfo);

        if (File.Exists(archivo_resultado))
        {
            System.Diagnostics.Debugger.Log(0, null, "archivo_resultado exists.");             
        }
        else
        {
            System.Diagnostics.Debugger.Log(0, null, "archivo_resultado doesn't exist.");
        }

        string returnedLine = "";
        try
        {
            System.Diagnostics.Debugger.Log(0, null, "Start - StreamReader to read archivo_resultado: " + archivo_resultado);
            using (StreamReader sr = new StreamReader(archivo_resultado))
            {
                returnedLine = sr.ReadLine() ?? "";
                Console.WriteLine(returnedLine);
                System.Diagnostics.Debugger.Log(0, null, "archivo_resultado: " + returnedLine);
            }
            System.Diagnostics.Debugger.Log(0, null, "Finished - StreamReader to read archivo_resultado: " + archivo_resultado);
        }
        catch (Exception Ex2)
        {
            System.Diagnostics.Debugger.Log(0, null, "The file could not be read.");
            Console.WriteLine(Ex2.Message);
        }
        string checkMacOutcome = "";
        var psi = new System.Diagnostics.ProcessStartInfo(ejecutable_CheckMac, archivo_temporal);
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardOutput = true;

        using (var proc = System.Diagnostics.Process.Start(psi))
        {
            using (StreamReader sr = proc.StandardOutput)
            {
                checkMacOutcome = sr.ReadToEnd();
            }
        }

        StreamWriter writetext = new StreamWriter(archivo_resultado);
        writetext.WriteLine(checkMacOutcome);
        writetext.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM