打开已在使用的文件

时间:2012-04-22 16:28:26

标签: 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);
    }

1 个答案:

答案 0 :(得分:0)

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