【问题标题】:The process cannot access the file '...' because it is being used by another process [duplicate]该进程无法访问文件'...',因为它正在被另一个进程使用[重复]
【发布时间】:2012-09-13 09:39:54
【问题描述】:

可能重复:
Error opening streamreader because file is used by another process

在 C# 中,我正在尝试构建一个程序,该程序将文本文件中列出的一定数量的文件或目录从一台计算机复制到同一网络上的另一台计算机(例如,从“\PC_OF_MARK”到“\PC_OF_SARAH” ) 但是,在打开和关闭 StreamReader 之后,我似乎无法使用流写入器打开流。

在我能够复制任何内容之前,我需要能够使用我的程序编辑“files.txt”的内容。

在我的表单上,我有 4 个按钮:添加、删除、关闭和复制。 我还有一个 ListBox lbItems,其中包含“files.txt”中的行。

程序位于删除函数中: 为了从“files.txt”中删除一行,我读取了 lbItems 的内容并将它们存储在一个列表中。接下来,我删除列表中与 lbItems 中的选定项具有相同索引的字符串。最后,我想通过打开 StreamWriter(这是发生错误的地方)并用新列表覆盖“files.txt”来更新“files.txt”。

“files.txt”包含这两行:

"%UserProfile%\Documents\files.txt" & "%UserProfile%\Downloads\RubiksCube"

public partial class Form1 : Form
{

    List<string> envKeys = new List<string>();
    List<string> envValues = new List<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IDictionary ev = Environment.GetEnvironmentVariables();
        foreach (DictionaryEntry de in ev)
        {
            envKeys.Add("%" + de.Key.ToString() + "%");
            envValues.Add(de.Value.ToString());
        }
        syncList();
    }

    private void syncList()
    {
        StreamReader r = new StreamReader(@"C:\Users\Eigenaar\Documents\files.txt");
        string line = r.ReadLine();
        while (line != "")
        {
            lbItems.Items.Add(line);
            line = r.ReadLine();
        }
        r.Close();
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {

        if (lbItems.SelectedIndex != -1)
        {
            if (MessageBox.Show("Ben je zeker dat je " + lbItems.Items[lbItems.SelectedIndex] + " uit de lijst wil verwijderen?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
            {
                List<string> array = new List<string>();
                using (StreamReader r = new StreamReader(@"C:\Users\Eigenaar\Documents\files.txt"))
                {
                    string line = r.ReadLine();
                    while (line != null)
                    {
                        array.Add(line);
                        line = r.ReadLine();
                    }
                    string t = r.ReadToEnd();
                }

                array.RemoveAt(lbItems.SelectedIndex);

                //Error occurs here:
                using (StreamWriter w = new StreamWriter(@"C:\Users\Eigenaar\Documents\files.txt", false))
                //The process cannot access the file 'C:\Users\Eigenaar\Documents\files.txt' because it is being used by another process.
                {
                    foreach (string str in array)
                    {
                        w.WriteLine(str);
                    }
                }
                lbItems.Items.RemoveAt(lbItems.SelectedIndex);
            }
        }
    } 
}

如果有人知道我在说什么,请帮忙!

【问题讨论】:

  • 你为什么不使用 File.Move ?
  • 你能举个例子吗,老爷?

标签: c# io stream streamreader streamwriter


【解决方案1】:

我认为你的问题出在第一个

using(StreamReader r = new StreamReader(@"C:\Users\Eigenaar\D...

using 应该会导致流被处理,但这可能无法足够快地关闭流,我会尝试在第一个 using 块的末尾添加r.Close()。这可能会确保手柄被释放,以便您可以再次打开它。

如果这不起作用,您可以尝试将文件作为读取共享打开,这样它就不会被锁定。

【讨论】:

  • 添加了 r.Close(),但我仍然在同一个位置遇到同样的错误。至于您的其他解决方案,我如何以读取共享的方式打开文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
相关资源
最近更新 更多