打开已在使用中的文件

本文关键字:文件 | 更新日期: 2023-09-27 18:31:58

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() 比您将释放文件以进行进一步的读取操作。