如何在使用 c# 打开时复制 pst 文件 [英] How to copy a pst file while it is open using c#

查看:22
本文介绍了如何在使用 c# 打开时复制 pst 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在打开 Outlook 的情况下使用 c# 复制 pst 文件?

Is it possible to copy a pst file using c# with outlook open?

这是我已经得到的代码,但它仍然给我错误:进程无法访问文件filepath",因为它正被另一个进程使用.

Here is the code i have got already but it still gives me the error :The process cannot access the file 'filepath' because it is being used by another process.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace outlookPSTCopy
{
class Program
{
    static void Main(string[] args)
    {
        string done = "the file is done copying";//done massage
        string copyFrom = args[0];
        string copyTo = args[1];
        Console.WriteLine(copyTo);
        Console.ReadLine();
        try
        {
            //start of test
            using (var inputFile = File.Open(copyFrom, FileMode.Open,    FileAccess.ReadWrite, FileShare.Read))
            {
                using (var outputFile = new FileStream(copyTo, FileMode.Create))
                {
                    var buffer = new byte[0x10000];
                    int bytes;

                    while ((bytes = inputFile.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        outputFile.Write(buffer, 0, bytes);
                    }
                }
            }



            //end of test

            //System.IO.File.Copy(copyFrom, copyTo, true);
        }
        catch (Exception copyError)
        {

            Console.WriteLine("{0} Second exception caught.", copyError);
        }

        Console.WriteLine("{0} ", done);


        Console.ReadLine();
    }
}
}

感谢您的帮助!

推荐答案

要创建被 Windows 上的另一个进程锁定的文件的副本,最简单(可能也是唯一)的解决方案是使用卷影复制服务 (VSS).

To create a copy of a file that is locked by another process on Windows, the simplest (and probably only) solution is to use the Volume Shadow Copy Service (VSS).

卷影复制服务很复杂,很难从托管代码中调用.幸运的是,一些优秀的人为此创建了一个 .NET 类库.查看 CodePlex 上的 Alpha VSS 项目:http://alphavss.codeplex.com.

The Volume Shadow Copy Service is complex and difficult to call from managed code. Fortunately, some fine chaps have created a .NET class library for doing just this. Check out the Alpha VSS project on CodePlex: http://alphavss.codeplex.com.

这篇关于如何在使用 c# 打开时复制 pst 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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