How to copy a pst file while it is open using c# [duplicate]

偶尔善良 提交于 2020-01-16 04:20:08

问题


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!


回答1:


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).

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.



来源:https://stackoverflow.com/questions/17537505/how-to-copy-a-pst-file-while-it-is-open-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!