【问题标题】:C# exception. File is being used by another processC# 异常。文件正被另一个进程使用
【发布时间】:2013-09-23 10:45:44
【问题描述】:

我在玩 C#,但遇到了一个问题。 当我尝试创建一个新文件时,程序会中断并说该文件正在被另一个进程使用。这可能是我忽略了一些愚蠢的事情,但我无法弄清楚!

这是我的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myNameSpace
{
    public partial class MainWindow : Form
    {
        public static string folderPath = @"fullpath";
        public MainWindow()
        {
            InitializeComponent();
            StoredTb.Text = folderPath;
            String[] files = Directory.GetFiles(folderPath);
            foreach (string file in files)
                myDropDown.Items.Add(Path.GetFileNameWithoutExtension(file));
        }

        private void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
        }

        private void myDropDown_invokedMethod()
        {
            string fullpath = MainWindow.folderPath + myDropDown.SelectedText + ".txt";
            StreamReader sr = new StreamReader(fullpath);
            NameTb.Text = myDropDown.SelectedText;
            DescriptionTb.Text = sr.ReadToEnd();
            sr.Close();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            File.Create(NameTb.Text + ".txt");
            TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); /* this is where the problem occurs */
            tw.WriteLine("The very first line!");
            tw.Close();
        }
    }
}

抱歉,sn-p 代码太长了,但由于我不确定问题出在哪里,所以我必须包含几乎所有内容。

【问题讨论】:

    标签: c# file process


    【解决方案1】:

    您的问题是File.Create 将打开一个stream 允许您对文件执行您喜欢的操作:

    http://msdn.microsoft.com/en-us/library/d62kzs03.aspx

    提供对路径中指定文件的读/写访问权限的 FileStream。

    因此,从技术上讲,它已经在使用了。

    只需完全删除 File.Create。如果文件不存在,StreamWriter 将处理创建文件。

    另外,投资using 构造以正确处理您的文件连接。将来有助于避免这种情况。

    【讨论】:

    • 谢谢!有效。我会在 6 分钟内接受(所以我不会接受)
    【解决方案2】:

    根据 MSDN,File.Create Method (String) 使用 FileStream,在您的情况下它没有被关闭。使用这样的东西:

    FileStream fs = new FileStream(NameTb.Text + ".txt");
    File.Create(fs);
    fs.Close();
    

    或@Muctadir 第纳尔

    var fileStream = File.Create(NameTb.Text + ".txt");
    //... do all the writing using fileStream
    fileStream.Close();
    

    【讨论】:

      【解决方案3】:

      使用

      myDropDown_invokedMethod();
      

      而不是

      this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
      

      【讨论】:

      • 谢谢!这不是导致异常的原因,但我会确保更改它。
      【解决方案4】:

      File.Create 返回一个包含您的文件的 FileStream 对象。您应该将此对象用于进一步的任务。

      var fileStream = File.Create(NameTb.Text + ".txt");
      //... do all the writing using fileStream
      fileStream.Close();
      

      或者你可以这样做

      var fs = File.Create(NameTb.Text + ".txt");
      fs.Close();
      TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); 
      tw.WriteLine("The very first line!");
      tw.Close();
      

      【讨论】:

        【解决方案5】:

        您的问题似乎出在SaveBtn_Click 事件中,您两次使用目标文件进行写入:

        File.Create(NameTb.Text + ".txt");
        TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); 
        

        删除这一行:

        File.Create(NameTb.Text + ".txt");
        

        【讨论】:

          【解决方案6】:

          那是因为 File.Create(NameTb.Text + ".txt"); 保持文件打开:

          尝试改用这个:

              private void SaveBtn_Click(object sender, EventArgs e)
              {
                  using(Stream stream = File.Create(NameTb.Text + ".txt"))
                  {
                      TextWriter tw = new StreamWriter(stream); /* this is where the problem was */
                      tw.WriteLine("The very first line!");
                      tw.Close();
                  }
              }
          

          当方法退出时,这将强制 File.Create 被处置。 dispose 将关闭文件句柄(win32 非托管句柄)。否则文件将被垃圾收集器关闭,但你永远不知道什么时候。

          【讨论】:

            【解决方案7】:

            我看到了两种方式: 1)使用 System.IO.File.WriteAllText http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx 2)阅读关于处置http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-03-10
              • 1970-01-01
              • 2018-01-30
              • 2015-10-24
              • 1970-01-01
              • 1970-01-01
              • 2013-09-14
              • 2011-04-13
              相关资源
              最近更新 更多