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!

Justin
  • 1,249
  • 1
  • 15
  • 37

1 Answers1

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.

STLDev
  • 5,950
  • 25
  • 36
  • Do you happen to have any examples? They seem to very little documentation. Thank you for your help! – Justin Jul 09 '13 at 14:45
  • 1
    You can find documentation on the Volume Shadow Copy Service here: http://msdn.microsoft.com/en-us/library/windows/desktop/bb968832(v=vs.85).aspx There is a sample project that utilizes AlphsVSS here: http://alphavss.codeplex.com – STLDev Jul 10 '13 at 02:28