Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式时,处理多个拖放文件失败_C# - Fatal编程技术网

C# 使用正则表达式时,处理多个拖放文件失败

C# 使用正则表达式时,处理多个拖放文件失败,c#,C#,在我的应用程序中,用户可以将多个文本文件拖放到GUI控件上,以将它们转换为另一种格式。以下是相关代码: private void panelConverter_DragDrop(object sender, DragEventArgs e) { string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string filename in filena

在我的应用程序中,用户可以将多个文本文件拖放到GUI控件上,以将它们转换为另一种格式。以下是相关代码:

    private void panelConverter_DragDrop(object sender, DragEventArgs e)
    {
        string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop);
        foreach (string filename in filenames)
        {
            convertFile(filename);
        }
    }

    private void convertFile(string filename)
    {
        // build name of output file
        string convertedFile = Path.ChangeExtension(filename, ".out");

        // open input file for reading
        FileInfo source = new FileInfo(filename);
        StreamReader srcStream = source.OpenText();

        // open output file for writing
        StreamWriter dstStream = new StreamWriter(convertedFile);

        // loop over input file
        string line;
        do
        {
            // get next line from input file
            line = srcStream.ReadLine();

            if (!Regex.IsMatch(line, @"fred=\d+"))
            {
                dstStream.WriteLine(line);
                dstStream.Flush();
            }
        } while (line != null);
    }

问题是,当我将多个文件放到GUI上时,实际上只处理其中一个文件。我发现如果我注释掉正则表达式行,所有删除的文件都会被处理。在这种情况下,我在处理正则表达式时是否遗漏了一些内容?

尝试以下方法的变体:

private void convertFile(string filename)
{
    // build name of output file
    string convertedFile = Path.ChangeExtension(filename, ".out");

    // open input file for reading
    FileInfo source = new FileInfo(filename);
    StreamReader srcStream = source.OpenText();

    // open output file for writing
    using (StreamWriter dstStream = File.CreateText(convertedFile))
    {
        // loop over input file
        string line;
        do
        {
            // get next line from input file
            line = srcStream.ReadLine();

            if (!Regex.IsMatch(line, @"fred=\d+"))
            {
                dstStream.WriteLine(line);
                dstStream.Flush();
            }
        } while (line != null);
    }

    Debug.WriteLine(string.Format("File written to: {0}", convertedFile));
}
主要修改是使用using关键字,这将保证文件资源的处理和关闭。如果问题仍未解决,请尝试以下操作:

  • 您有任何全局异常处理程序吗?确保选中调试>异常。。。因此VisualStudio会自动在引发异常的行上中断。请参阅操作指南
  • 确保文件写在正确的位置。如果文件具有完整路径,则上面的Debug.WriteLine语句将告诉您文件正在被写入

如果没有异常发生,您应该在磁盘上写入至少0长度的文件。

很好。启用Debug>Exceptions选项,VS现在在解析第一个输入文件时报告空引用。这是因为输入文件中有一个尾随的新行。这导致正则表达式调用barf。我在Regex调用周围添加了一个null测试,现在所有文件都得到了正确处理。谢谢你在这方面的帮助。