【问题标题】:File is being used by another process except that it's not文件正在被另一个进程使用,但它不是
【发布时间】:2013-07-01 09:58:30
【问题描述】:

我目前正在开发一个实用程序来解析多个xml 文件并将结果写入csv 文件。在倒数第二行(代码)我得到错误:

The process cannot access the file 'W:\SRC\hDefML\myExcelFile.csv' because it is being used by another process.'.

有人可以帮我吗,因为我不知道出了什么问题,该文件没有被其他任何东西使用,这让我发疯了吗?

这是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace GenNameUtility
{
    class NameGenerator
    {
        static void Main(string[] args)

        {
            var files = from file in       Directory.GetFiles("W:\\SRC\\hDefMl\\1.0\\Instrument_Files") orderby file 
        ascending select file;

        StringBuilder sb_report = new StringBuilder();

        string delimiter = ",";

        sb_report.AppendLine(string.Join(delimiter, "Module", "Generator(s)"));

        foreach (var file in files)
        {
            string filename = Path.GetFileNameWithoutExtension(file);

            Console.Write("The HDefML file for {0} contains these EEPROM Generators:", filename);

            XDocument hdefml = XDocument.Load(file);

                var GeneratorNames = from b in hdefml.Descendants("Generators") select new
        {
           name = (string)b.Element("GeneratorName") 

        }.ToString();

        StringBuilder sb = new StringBuilder();
        foreach (var item in GeneratorNames)
        {
            Console.Write("  GeneratorName is: {0}", GeneratorNames);
            sb_report.AppendLine(string.Join(delimiter, filename, GeneratorNames));

        var hdef = File.Create(@"W:\SRC\hDefML\myExcelFile.csv").ToString();
        File.WriteAllText(hdef, sb.ToString());
        }          
     }
        Console.ReadLine();
}
}

}

【问题讨论】:

    标签: c# xml visual-studio-2010 linq parsing


    【解决方案1】:

    您需要在写入文件后关闭该文件。见using

    另外,最好在循环之前打开文件并在之后关闭它。

    【讨论】:

    • 当我得到一个错误说不能将 FileStream 转换为字符串?也感谢您的帮助。 @西蒙
    • File.WriteAllText 需要以下参数:文件名和要写入的文本。您混合了几种写入数据的方式。我建议这样做:using (StreamWriter sw=File.CreateText(@"...")) { foreach(...) { ... sw.Write(sb.ToString()); ... } }
    【解决方案2】:

    该文件正被另一个进程使用...但该进程实际上是你的。

    File.Create 返回一个FileStream。您正在打开文件.. 写入文件.. 但没有关闭它。当新的迭代到来时..文件仍然打开。

    你可以试试这样的:

    using (var file = File.Create(@"W:\SRC\hDefML\myExcelFile.csv")) {
        // write content here using file
    } // this closes the file automatically.
    

    不过按照建议,我会将上述内容包装在循环之外,这样您就不会经常打开和关闭文件。

    【讨论】:

      【解决方案3】:

      File.WriteAllText 将为您创建一个文件,因此无需事先使用File.Create

      File.WriteAllText(@"W:\SRC\hDefML\myExcelFile.csv", sb.ToString());
      

      您的File.Create 流似乎锁定了文件,这就是File.WriteAllText 抛出错误的原因。

      如果您需要使用File.Create,您可以使用StreamWriter 将其写出来。

      using(var fs = File.Create(@"W:\SRC\hDefML\myExcelFile.csv"))
      using (StreamWriter sw = new StreamWriter(fs))
      {
          sw.Write(sb.ToString());                  
      }
      

      顺便说一句,上面的using格式和做的一样

      using(var fs = File.Create(@"W:\SRC\hDefML\myExcelFile.csv"))
      {
         using (StreamWriter sw = new StreamWriter(fs))
         {
            sw.Write(sb.ToString());                  
         }
      }
      

      所以你可以使用任何你觉得更易读的。

      【讨论】:

      • 这很高兴知道,但在这种情况下,首先创建文件似乎更好。谢谢。
      • @Jason 没问题,我已经更新了我的答案。正如其他人建议的那样,您应该使用using 语句。但是,因为您使用的是FileStream,所以不能简单地向其写入文本。您可以使用StreamWriter 为您将文本写入 FileStream。
      • 另外,您的foreach 循环每次都调用Create,这将覆盖之前创建的文件。您可以改用File.AppendText 或更改循环中创建的文件的名称。
      • 循环覆盖每个文件的事实是否是当我运行它然后查看文件时没有任何内容的原因?
      • 如果循环中最后一项中sb.ToString() 的值为空,那么是的,这可能就是原因。
      猜你喜欢
      • 2015-11-26
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多