访问C#中另一个进程正在使用的文件 [英] Access to a file which is being used by another process in C#

查看:241
本文介绍了访问C#中另一个进程正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

因此,存在一个.gdb文件,该文件正在由应用程序使用.我想在使用它时从中阅读.但是我当然不能.我尝试复制它,然后访问,但是使用File.Copy再次引发异常文件正在被另一个进程使用".但是只需在Windows(ctrl + c)中复制此文件并粘贴(ctrl + v),它就可以工作.那么File.Copy和普通ctrl + c ctrl + v方法之间有什么区别?
我该如何实现呢?我已经阅读了有关卷影复制的信息,但没有找到任何C#代码.

Hello!

So there is a .gdb file, which is being used by an application. I would like to read from it while it is being used. But of course I can''t. I tried copying it, and then accessing, but with File.Copy it threw an exception "File is being used by another process" again. But by simply copying this file in windows (ctrl+c) and pasting (ctrl+v) it worked. So what''s the difference between File.Copy and the normal ctrl+c ctrl+v method?
And how could I implement this? I have read about Shadow Copy, but I didn''t find any C# code.

推荐答案

如果文件以排他锁打开(表示未共享)进行读取),那么您将无法使用任何常规API打开文件.可能有一些较低级别的解决方案可以直接从磁盘读取,但是我认为它们都不是C#的实用解决方案.
If the file is opened with an exclusive lock (meaning it was not shared for read), then you will not be able to use any of the regular APIs to open the file. There may be lower level solutions that can read directly off the disk, but I don''t think any of them are practical solutions from C#.


好吧,我找到了一个解决方案:
All right, I found a solution:
FileStream inf = new FileStream("path1", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            FileStream outf = new FileStream("path2", FileMode.Create);
            int a;
            while ((a = inf.ReadByte()) != -1)
            {
                outf.WriteByte((byte)a);
            }
            inf.Close();
            inf.Dispose();
            outf.Close();
            outf.Dispose();


尝试一下,这是在CP上提出的类似问题.
IOException:进程无法访问文件文件名 [ ^ ]
认为这可能会有所帮助
Try this, This is similar type of question which was asked on CP.
IOException: The Process can not access the file filename[^]
Thought this might be helpful


这篇关于访问C#中另一个进程正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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