【问题标题】:file is using by another process文件正在被另一个进程使用
【发布时间】:2013-01-31 11:33:17
【问题描述】:

我是 C# 编程的新手。我在使用FileStream 时遇到问题。我想通过在数据库中搜索人的 ID 从数据库中检索图片。它的工作原理!但是当我尝试两次检索该人的图片时(两次插入相同的 ID)。它会给出 IOException

"The process cannot access the file 'C:\Users\dor\Documents\Visual Studio 2010\Projects\studbase\studbase\bin\Debug\foto.jpg' because it is being used by another process."

我有 1 个按钮 --> buttonLoad | 1 个图片框 --> pictureBoxPictADUStudent

这是 buttonLoad 上的代码

        string sql = "SELECT Size,File,Name FROM studgeninfo WHERE NIM = '"+textBoxNIM.Text+"'";
        MySqlConnection conn = new MySqlConnection(connectionSQL);
        MySqlCommand comm = new MySqlCommand(sql, conn);

        if (textBoxNIM.Text != "")
        {
            conn.Open();
            MySqlDataReader data = comm.ExecuteReader();

            while (data.Read())
            {

                int fileSize = data.GetInt32(data.GetOrdinal("size"));
                string name = data.GetString(data.GetOrdinal("name"));


                using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
                {
                        byte[] rawData = new byte[fileSize];
                        data.GetBytes(data.GetOrdinal("file"), 0, rawData, 0, fileSize);
                        fs.Write(rawData, 0, fileSize);
                        fs.Dispose();
                        pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);
                }
            }

            conn.Close();
        }

        else
        {
            MessageBox.Show("Please Input Student NIM ", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

【问题讨论】:

    标签: c# file-io io


    【解决方案1】:

    您正在这里打开文件:

    using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
                                       // ^^^^ This is your filename..
    

    ..和Bitmap 也试图打开文件来阅读它.. 这里:

    pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);
                                                       // ^^^^ You are using it again here
    

    Bitmap 在您写入文件时将无法读取文件..

    编辑:

    正如 cmets 中所指出的,即使正在调用 fs.Dispose(),也会发生这种情况。见这里:Does FileStream.Dispose close the file immediately?

    【讨论】:

    • 但是他不是叫fs.Dispose()上面一行吗?为什么不释放文件句柄?
    • 这可能是由于“using”的内部处理造成的,即使您手动处理 fs,也会保留 fs 直到离开代码块
    • @Jason 其实我刚刚测试过这个,似乎每次都不起作用..
    • @BennorMcCarthy 这似乎是我正在经历的。
    【解决方案2】:

    这里的问题是文件被new Bitmap()锁定了。所以你必须确保一旦加载了位图,它对文件的锁定就被释放了:

            using (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))
            {
                    byte[] rawData = new byte[fileSize];
                    data.GetBytes(data.GetOrdinal("file"), 0, rawData, 0, fileSize);
                    fs.Write(rawData, 0, fileSize);
            }
    
            using (var bmpTemp = new Bitmap(name))
            {
                pictureBoxPictADUStudent.BackgroundImage= new Bitmap(bmpTemp);
            } 
    

    更新: 我更新了我的答案以反映您的最新答案。 更多详细信息请转至this post

    【讨论】:

    • fs.Dispose() 现在是多余的,可以删除。
    • 我试过了。但它总是像以前一样给我 IOException 错误。
    • 对于第一次搜索,它是工作。但是当我再次搜索时(不关闭应用程序)它给了我同样的错误
    • @eliezeramon 你确定没有在其他地方访问资源吗?
    • 我删除了它。但仍然在 FileStream fs = new FileStream(name,FileMode.Create,FileAccess.Read); 上给出 IOException
    【解决方案3】:

    这样写 使用 (FileStream fs = new FileStream(name, FileMode.Create, FileAccess.Write))

    {


    } pictureBoxPictADUStudent.BackgroundImage = new Bitmap(name);

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多