【问题标题】:C# read each line from text file and update label with each lineC# 从文本文件中读取每一行并用每一行更新标签
【发布时间】:2014-04-23 03:43:57
【问题描述】:

我有一个 C# 程序,它应该逐行遍历日志文件,然后根据读取的内容更新标签,但是,我得到了一个未处理的异常。

它正在读取的文件来自另一个正在写入日志的外部 c++ 程序(我希望这个 C# 包装程序能够将输出从文本重定向到这个窗口(这是我能想到的唯一方法of 是在 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();

这里是例外(发生两次)-

在 PresentationFramework.dll 中发生了“System.Windows.Markup.XamlParseException”类型的第一次机会异常

附加信息:对匹配指定绑定约束的“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).

【问题讨论】:

  • 您确定异常发生在您从日志中读取时,而不仅仅是在创建窗口时?把阅读器代码注释掉看看?
  • 该异常似乎与 XAML / WPF 有关,它是您应用的演示(显示)框架。此代码是否在主窗口的构造函数中?也许尝试注释掉这段代码并确保程序运行,或者将代码移动到控制台应用程序以隔离。
  • 另外 - 而不是读取文件(可能存在争用问题) - 您可以让您的 CMD 简单地将其结果回显到标准输出并使用您的 Process 对象的 .StandardOutput 属性(@987654324 @) 这是一个 StreamReader:msdn.microsoft.com/en-us/library/…
  • 问题似乎是同一实例运行的后台进程太多,我不小心忘记关闭。但是,我现在的问题是它不会实时更新标签,只会更新一次。反正有实时更新吗?
  • 但是您只读取文件一次。一种选择可能是使用 FileSystemWatcher。

标签: c# .net wpf io streamreader


【解决方案1】:

可能是 C++ 应用程序已锁定文件以进行独占访问。您是否有权访问源代码,以便在必要时检查和更改它?如果是这样,请确保它以共享访问权限打开文件(不确定 C++ 中的样子)。如果您随后设法在 WPF 应用程序中打开该文件,则需要确保以共享访问权限打开它,这样您就不会阻止 C++ 应用程序对其进行访问。看一下FileStream(最后一个参数FileStream(fileName, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read))。

我还建议将您的代码移出 Window 构造函数(我假设它基于异常)并将其包装在异常处理中,以便您可以获得更有意义的异常,我猜是 @ 987654322@.

其他一些考虑因素是,当您的流阅读器赶上写作时会发生什么?您的循环将结束,您将在标签中仅看到日志文件的最后一行(当时)。您可能需要更精细的解决方案来监控文件的更改或文件的长度以及您当前的索引。

【讨论】:

    【解决方案2】:

    我设法通过从“\\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();
        }
    }
    

    问候,
    尤金

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2018-08-24
      • 2022-10-04
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多