c# - 打开一个已经在使用中的文件

标签 c# .net file filestream ioexception

using (System.IO.FileStream fs = File.Open(GetCurrentWallpaper(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {

我正在编写一个应用程序,每次更改时都需要像这样打开当前墙纸。 我首先访问注册表以获取墙纸的路径 (GetCurrentWallpaper),并在墙纸更改时使用 FileSystemWatcher 对其进行处理。 奇怪的是,它只能工作一次。如果第二次访问墙纸(无论我等待多长时间),我的应用程序都会崩溃并出现 IOException 告诉我无法访问该文件,因为它已在使用中。 如果我重新启动应用程序,它可以再次访问该文件,但如上所述,只能访问一次。否则它会崩溃。 我可以做些什么来访问该文件吗?

编辑:更多代码:

            using (System.IO.FileStream fs = File.Open(GetCurrentWallpaper(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                using (Bitmap orig = (Bitmap)Bitmap.FromStream(fs, true, false)) {
                    int width = Convert.ToInt32(orig.Width / 3);
                    int height = Convert.ToInt32(orig.Height / 3);

                    Rectangle rect = new Rectangle(0, 0, width, height);
                    using (Bitmap bmp = new Bitmap(width, height)) {
                        using (Graphics bmpg = Graphics.FromImage(bmp)) {
                            col = ColorHelper.CalculateAverageColor(bmp, true, 20);
                            fs.Flush();
                            fs.Close();
                        }
                    }
                }
            }

//this is in the ColorHelper class
public static System.Drawing.Color CalculateAverageColor(Bitmap bm, bool dropPixels, int colorDiff) {
        int width = bm.Width;
        int height = bm.Height;
        int red = 0;
        int green = 0;
        int blue = 0;
        int minDiversion = colorDiff;
        int dropped = 0;
        long[] totals = new long[] { 0, 0, 0 };
        int bppModifier = bm.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb ? 3 : 4;

        BitmapData srcData = bm.LockBits(new System.Drawing.Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, bm.PixelFormat);
        int stride = srcData.Stride;
        IntPtr Scan0 = srcData.Scan0;

        unsafe {
            byte* p = (byte*)(void*)Scan0;

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    int idx = (y * stride) + x * bppModifier;
                    red = p[idx + 2];
                    green = p[idx + 1];
                    blue = p[idx];

                    if (dropPixels) {
                        if (Math.Abs(red - green) > minDiversion || Math.Abs(red - blue) > minDiversion || Math.Abs(green - blue) > minDiversion) {
                            totals[2] += red;
                            totals[1] += green;
                            totals[0] += blue;
                        } else {
                            dropped++;
                        }
                    } else {
                        totals[2] += red;
                        totals[1] += green;
                        totals[0] += blue;
                    }
                }
            }
        }

        int count = width * height - dropped;
        int avgR;
        int avgG;
        int avgB;

        if (count > 0) {
            avgR = (int)(totals[2] / count);
            avgG = (int)(totals[1] / count);
            avgB = (int)(totals[0] / count);
        } else {
            avgR = (int)(totals[2]);
            avgG = (int)(totals[1]);
            avgB = (int)(totals[0]);
        }

        bm.UnlockBits(srcData);
        return System.Drawing.Color.FromArgb(avgR, avgG, avgB);
    }

最佳答案

如果您使用 fs.Close(),那么您将释放文件以进行进一步的读取操作。

关于c# - 打开一个已经在使用中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10269768/

相关文章:

c# - 简单的喷油器诊断警告一次性瞬变

c# - DataGridView 不显示数据库中带有空图像的所有值

file - Grails - 从 Quartz 作业和过滤器记录日志

c# - t4 模板的缩进错误

c# - Index 的其他原因超出了 .Net 字典中的数组范围

C# DateTime ParseExact 异常

c# - list<> 是否存储对属性或实例的引用?

c# - 我是否必须取消订阅局部变量的匿名事件处理程序?

C++ 独立可执行文件

c# - 如何在 C# 中使用热键启动程序?