简体   繁体   English

检查未锁定的文件是否打开

[英]Check if a file that's not locked is open

Some programs (image programs such as Paint, text editors such as notepad and Wordpad,and others) open files, load the contents into memory, then release the file lock. 某些程序(图像程序(例如Paint),文本编辑器(如记事本和Wordpad等)和其他程序)打开文件,将内容加载到内存中,然后释放文件锁。 Is there a way to tell if a program is using that file even though it's not locked? 有没有办法判断一个程序是否正在使用该文件,即使它没有被锁定?

For example, even if image1.bmp is open in Paint, my program can overwrite the copy of image1.bmp that's on the disk because the file isn't locked. 例如,即使在Paint中打开image1.bmp,我的程序也可以覆盖磁盘上image1.bmp的副本,因为该文件未锁定。 Now the copy of image1.bmp that is open in Paint is different than the copy of image1.bmp that is on the disk. 现在,在Paint中打开的image1.bmp的副本不同于磁盘上的image1.bmp的副本。

My program is written in C#. 我的程序是用C#编写的。 I usually use this method for checking if a file is locked, but it won't work in the above case. 我通常使用这种方法检查文件是否被锁定,但在上述情况下将无法使用。 Is there a way to check if a file is in use? 有没有办法检查文件是否正在使用?

Is there any solution to this? 有什么解决办法吗?

"Now the copy of image1.bmp that is open in Paint" - here's your mistake - the file is no longer open in Paint. “现在在Paint中打开了image1.bmp的副本” -这是您的错误-该文件不再在Paint中打开。 It was opened, read, and then closed. 它被打开,读取,然后关闭。 Paint does not keep the file open at all - it only has a COPY of its contents in RAM memory. Paint根本不会使文件保持打开状态-在RAM内存中只有其内容的副本。 To put it in another way - the fact that you see a picture in MS Paint doesn't mean the file is open. 换句话说,在MS Paint中看到图片并不意味着文件已打开。

It is comparable to loaning a document to someone, then he makes a photocopy and returns it - that person no longer "holds" the document, he has a separate copy of it. 这相当于将文件借给某人,然后他进行复印并归还-该人不再“持有”该文件,而他拥有该文件的单独副本。 And there is no way, just by looking at the document, to know who might have made a copy of it at some point in history. 而且,仅通过查看文档就无法知道谁在历史的某个时刻复制了该文档。

Another way of putting it is this pseudocode: 放置它的另一种方法是此伪代码:

File file = Open("image.png");
Image img = ImageFromFile(file);
file.Close();

...

img.Save("image.png");

Here no file is being opened at all, there's just a copy in RAM of its content. 这里根本没有打开任何文件,RAM中只有一个副本。

Note: I actually checked that for Paint - Process Explorer can show you opened handles, I opened a file in Paint and there was no handle at all listed for a file of that name. 注意:实际上,我检查了对于Paint-Process Explorer是否可以显示打开的句柄,我在Paint中打开了一个文件,对于该名称的文件,根本没有列出任何句柄。

Here's what I came up with. 这是我想出的。 I check all open processes for a window title. 我检查所有打开的进程的窗口标题。 If the process has a window title, I see if it contains the name of the file I'm looking for. 如果该进程具有窗口标题,那么我将查看它是否包含我要查找的文件的名称。

It won't work 100% of the time since some applications can have multiple files open in a single instance. 由于某些应用程序可以在单个实例中打开多个文件,因此无法100%地工作。

I adapted it from this question: Getting a list of all applications 我根据以下问题进行了调整: 获取所有应用程序的列表

        bool isFileOpen(string file)
          {    
            string windowTitle = "";


            Process[] myProcesses = Process.GetProcesses();

            foreach (Process P in myProcesses)
            {
                if (P.MainWindowTitle.Length > 1)
                {
                    windowTitle = P.MainWindowTitle;
                    if (windowTitle.Contains(file) == true)
                    {
                        return true;                        
                    }                    
                }

            }
            return false;

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM