C#从文本文件中读取每一行,并用每行更新标签

时间:2014-03-17 18:47:39

标签: c# .net wpf io streamreader

我有一个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).

2 个答案:

答案 0 :(得分:0)

可能是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

其他一些注意事项是,当您的流阅读器赶上写作时会发生什么?您的循环将结束,您最终只会在标签中的日志文件的最后一行(此时)结束。您可能需要一个更复杂的解决方案来监视文件的更改或文件的长度以及当前索引。

答案 1 :(得分:0)

我设法通过删除" \\"来设法让您的代码正常工作来自" \\ log.txt" +"&& p.HasExited == false"因为我没有测试过程。

几乎忘了删除" \\"来自" p.StartInfo.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();
    }
}

的问候,
尤金