2012-07-31 114 views
2

我想删除行末尾的空格,然后将该行写入另一个文件。文件无法访问,因为它正在被另一个程序使用

但是当程序到达FileWriter然后它给我下面的错误,因为它正由另一个进程

过程不能被访问。

该代码如下。

private void FrmCounter_Load(object sender, EventArgs e) 
{ 
     string[] filePaths = Directory.GetFiles(@"D:\abc", "*.txt", SearchOption.AllDirectories); 
     string activeDir = @"D:\dest"; 
     System.IO.StreamWriter fw; 
     string result; 

     foreach (string file in filePaths) 
     { 
      result = Path.GetFileName(file); 
      System.IO.StreamReader f = new StreamReader(file); 
      string newFileName = result; 
      // Combine the new file name with the path 
      string newPath = System.IO.Path.Combine(activeDir, newFileName); 
      File.Create(newPath); 

      fw = new StreamWriter(newPath); 

      int counter = 0; 
      int spaceAtEnd = 0; 
      string line; 

      // Read the file and display it line by line. 
      while ((line = f.ReadLine()) != null) 
      { 
       if (line.EndsWith(" ")) 
       { 
        spaceAtEnd++; 
        line = line.Substring(0, line.Length - 1); 
       } 
       fw.WriteLine(line); 
       fw.Flush(); 
       counter++; 
      } 

      MessageBox.Show("File Name : " + result); 
      MessageBox.Show("Total Space at end : " + spaceAtEnd.ToString()); 
      f.Close(); 
      fw.Close(); 
     } 
} 
+0

那么,该文件显然是由另一个程序打开的。如果它作为窗口打开,也许尝试关闭它?也许另一个程序打开就像你正在尝试? – SimpleVar 2012-07-31 12:08:54

+0

有人猜测,因为您将activeDir和filePaths设置为相同的目录,您正试图写入正在读取的同一文件。 – StuartLC 2012-07-31 12:10:06

+0

那么我试着另一个目录..但同样的错误 – 2012-07-31 12:11:28

回答

2

File.Create本身返回流。

使用该流写入文件。您收到此错误的原因是因为File.Create返回的流处于打开状态,您正试图再次打开该文件进行写入。

要么关闭,对文件的写入流中File.Create或更好地利用返回的流或使用

Stream newFile = File.Create(newPath); 
fw = new StreamWriter(newFile); 
0

您必须先关闭您正在阅读的任何文件,然后才能尝试向您的案例写信。

0

编辑:

扎法尔是正确的,但是,也许这将清除事情。

由于File.Create返回一个流..该流打开了您的目标文件。这将使事情更清晰:

File.Create(newPath).Close(); 

使用上面的行,使其工作,但是,我会建议重新写入正确。这仅用于说明目的。 using语句像

+0

你是对的西蒙,但我编辑目标路径,以为我得到相同的错误 – 2012-07-31 12:16:02

+0

你能告诉我们你的编辑代码? – 2012-07-31 12:16:36

+0

更改发布后。 – 2012-07-31 12:19:14

0

写流:

using (System.IO.StreamReader f = new StreamReader(file)) 
{ 
    //your code goes here 
} 
1

即使你解决了你最初的问题,如果你想要的一切写入在原来的位置的新文件,您可以尝试将所有数据读入数组并关闭原始的StreamReader。性能注意事项:如果你的文件足够大,这个选项不会是最好的性能。

而且你不需要File.Create作为StreamWriter将创建一个文件,如果它不存在,或默认覆盖它,或者如果你指定append参数为false

result = Path.GetFileName(file); 
String[] f = File.ReadAllLines(file); // major change here... 
             // now f is an array containing all lines 
             // instead of a stream reader 

using(var fw = new StreamWriter(result, false)) 
{ 
    int counter = f.Length; // you aren't using counter anywhere, so I don't know if 
          // it is needed, but now you can just access the `Length` 
          // property of the array and get the length without a 
          // counter 

    int spaceAtEnd = 0; 

    // Read the file and display it line by line. 
    foreach (var item in f) 
    { 
     var line = item; 

     if (line.EndsWith(" ")) 
     { 
      spaceAtEnd++; 
      line = line.Substring(0, line.Length - 1); 
     } 
     fw.WriteLine(line); 
     fw.Flush(); 
    } 
} 

MessageBox.Show("File Name : " + result); 
MessageBox.Show("Total Space at end : " + spaceAtEnd.ToString()); 

此外,您不会使用此方法从行尾删除多个空格。如果您需要这样做,请考虑用line = line.TrimEnd(' ');替换line = line.Substring(0, line.Length - 1);

相关问题