1

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

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();
    }
}
}

Thank you for your help!

4

1 回答 1

1

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

卷影复制服务复杂且难以从托管代码中调用。幸运的是,一些优秀的小伙子已经创建了一个 .NET 类库来执行此操作。查看 CodePlex 上的Alpha VSS项目:http: //alphavss.codeplex.com

于 2013-07-08T23:31:42.330 回答