等待文件由进程被释放 [英] Wait for file to be freed by process

查看:292
本文介绍了等待文件由进程被释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何等待文件是免费的,这样 ss.Save()可以用一个新的覆盖它。如果我运行这两次并拢(ISH)我收到了通用GDI + 错误。

  ///<总结>
    ///抓斗的应用程序的屏幕截图,并将其保存在JPG C盘
    ///< /总结>
    私人静态字符串GetDesktopImage(DevEx press.XtraEditors.XtraForm whichForm)
    {
        矩形范围= whichForm.Bounds;        //这个解决我的问题,而是创建了一个混乱的问题
        // VAR的timeStamp = DateTime.Now.ToString(DDD-MMM-DD-YYYY-HH-MM-SS);
        //变种文件名=C:\\\\ HelpMe+ +的timeStamp.JPG;        变种文件名=C:\\\\ HelpMe.jpg
        File.Create(文件名);
        使用(位图SS =新位图(bounds.Width,bounds.Height))
        使用(图形G = Graphics.FromImage(SS))
        {
            g.CopyFromScreen(whichForm.Location,Point.Empty,bounds.Size);
            ss.Save(文件名,ImageFormat.Jpeg);
        }        返回文件名;
    }


解决方案

这样的功能会做到这一点:

 公共静态布尔IsFileReady(字符串sFilename)
    {
        //如果文件可以被打开用于独占访问它意味着该文件
        //被另一个进程不再锁定。
        尝试
        {
            使用(的FileStream的InputStream = File.Open(sFilename,FileMode.Open,FileAccess.Read,FileShare.None))
            {
                如果(inputStream.Length大于0)
                {
                    返回true;
                }
                其他
                {
                    返回false;
                }            }
        }
        赶上(例外)
        {
            返回false;
        }
    }

坚持在一个while循环,你有东西,将阻塞,直到该文件是可访问的。

How do I wait for the File to be Free so that ss.Save() can overwrite it with a new one. If I run this twice close together(ish) I get a generic GDI+ error.

    ///<summary>
    /// Grabs a screen shot of the App and saves it to the C drive in jpg
    ///</summary>
    private static String GetDesktopImage(DevExpress.XtraEditors.XtraForm whichForm)
    {
        Rectangle bounds = whichForm.Bounds;

        // This solves my problem but creates a clutter issue
        //var timeStamp = DateTime.Now.ToString("ddd-MMM-dd-yyyy-hh-mm-ss");
        //var fileName = "C:\\HelpMe" + timeStamp + ".jpg";

        var fileName = "C:\\HelpMe.jpg";
        File.Create(fileName);
        using (Bitmap ss = new Bitmap(bounds.Width, bounds.Height))
        using (Graphics g = Graphics.FromImage(ss))
        {
            g.CopyFromScreen(whichForm.Location, Point.Empty, bounds.Size);
            ss.Save(fileName, ImageFormat.Jpeg);
        }

        return fileName;
    }

解决方案

A function like this will do it:

    public static bool IsFileReady(String sFilename)
    {
        // If the file can be opened for exclusive access it means that the file
        // is no longer locked by another process.
        try
        {
            using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                if (inputStream.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
        catch (Exception)
        {
            return false;
        }
    }

Stick it in a while loop and you have something which will block until the file is accessible

这篇关于等待文件由进程被释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆