用c#读取一个txt文件

本文关键字:一个 txt 文件 读取 | 更新日期: 2023-09-27 18:10:21

我有一个。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);
        }

用c#读取一个txt文件

        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();