在C#中读取一个txt文件
Read a txt file in C#
我有一个 .aspx 页面,在代码隐藏文件中包含一些 C# 代码。
我想在命令提示符下执行命令并将输出保存到 .txt 文件。这有效并创建了一个新的 .txt 文件。
然后我想从 .txt 文件中读取第一行并将字符串存储在变量中。
我当前的代码如下,但是,它在我的代码中抛出 "The file could not be read." 错误消息。为什么会这样,我该如何解决?
更新:
所以我修改了我原来的 post 的代码,并添加了一个检查以查看 '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();
我有一个 .aspx 页面,在代码隐藏文件中包含一些 C# 代码。
我想在命令提示符下执行命令并将输出保存到 .txt 文件。这有效并创建了一个新的 .txt 文件。
然后我想从 .txt 文件中读取第一行并将字符串存储在变量中。
我当前的代码如下,但是,它在我的代码中抛出 "The file could not be read." 错误消息。为什么会这样,我该如何解决?
更新: 所以我修改了我原来的 post 的代码,并添加了一个检查以查看 '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();