c#从文本文件中读取每一行并更新每一行的标签
本文关键字:一行 更新 标签 文本 文件 读取 | 更新日期: 2023-09-27 17:50:44
我有一个c#程序,它应该逐行遍历日志文件,然后根据它所读取的内容更新标签,然而,我得到了一个未处理的异常。
它正在读取的文件来自另一个外部c++程序,该程序正在写入日志(我希望这个c#包装程序能够将输出从文本重定向到此窗口),我能想到的唯一方法是从c#中读取日志,因为c++程序写入日志。
代码如下:
p = new Process();
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "''scan.cmd";
p.Start();
//p.WaitForExit();
// Read the file and display it line by line.
string logFile = AppDomain.CurrentDomain.BaseDirectory + "''log.txt";
string line;
System.IO.StreamReader file = new System.IO.StreamReader(logFile);
while((line = file.ReadLine()) != null && p.HasExited == false)
{
itemBeiingScanned_Label.Content = line;
}
file.Close();
这里是例外(出现两次)-
下面是调试代码:类型为"System.Windows.Markup"的第一次机会异常。XamlParseException'发生在PresentationFramework.dll
附加信息:在类型'SpywareKing '上调用构造函数。与指定的绑定约束匹配的MainWindow'抛出异常。
如果有此异常的处理程序,则可以安全地继续执行程序。
64e35'PresentationFramework.dll'. Symbols loaded.
The thread 0x20c0 has exited with code 259 (0x103).
The thread 0x13b0 has exited with code 0 (0x0).
The thread 0x383c has exited with code 0 (0x0).
The thread 0x2838 has exited with code 259 (0x103).
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:'Users'Jaycen'Documents'Dev'SpywareKing'SpywareKing'bin'x64'Release'SpywareKing.exe'. Symbols loaded.
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:'windows'Microsoft.Net'assembly'GAC_MSIL'Accessibility'v4.0_4.0.0.0__b03f5f7f11d50a3a'Acc essibility.dll'. Symbols loaded.
SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded C:'windows'Microsoft.Net'assembly'GAC_MSIL'System.Configuration'v4.0_4.0.0.0__b03f5f7f11d50a3a'System.Configuration.dll'. Symbols loaded.
'SpywareKing.vshost.exe' (CLR v4.0.30319: SpywareKing.vshost.exe): Loaded 'C:'windows'Microsoft.Net'assembly'GAC_MSIL'PresentationFramework.Aero2'v4.0_4.0.0.0__31bf3856ad364e35'PresentationFramework.Aero2.dll'. Symbols loaded.
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'SpywareKing.MainWindow' that matches the specified binding constraints threw an exception.
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: The invocation of the constructor on type 'SpywareKing.MainWindow' that matches the specified binding constraints threw an exception.
The program '[14348] SpywareKing.vshost.exe' has exited with code 0 (0x0).
可能是c++应用程序锁定了文件以进行独占访问。您是否可以访问源代码,以便在必要时进行检查和更改?如果是这样,确保它以共享访问打开文件(不确定在c++中是什么样子)。如果您设法在WPF应用程序中打开该文件,则需要确保以共享访问方式打开它,这样就不会阻止c++应用程序对它的访问。看看FileStream(最后一个参数FileStream(fileName, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read)
)。
我还建议将您的代码从Window构造函数(我假设它是基于异常)中移出,并将其包装在异常处理中,以便您可以获得更有意义的异常,我猜是System.IO.IOException : The process cannot access the file 'log.txt' because it is being used by another process
。
其他一些需要考虑的问题是,当流阅读器赶上写入时会发生什么?循环将结束,标签中只剩下日志文件的最后一行(当时)。您可能需要一个更精细的解决方案来监视文件的更改,或者监视文件的长度和当前索引。
我设法通过从"''log.txt" + "&&p. hasexited == false"因为我没有进程来测试。
差点忘了从"p. startinfo . info ."中删除"''"。FileName = AppDomain.CurrentDomain.BaseDirectory + "''scan.cmd";"
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Process p = new Process();
try
{
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "''scan.cmd";
p.Start();
p.WaitForExit();
}
catch (Exception exception)
{
}
// Read the file and display it line by line.
string logFile = AppDomain.CurrentDomain.BaseDirectory + "log.txt";
string line;
System.IO.StreamReader file = new System.IO.StreamReader(logFile);
while ((line = file.ReadLine()) != null)
{
itemBeiingScanned_Label.Content = line;
}
file.Close();
}
}
问候,
尤金