问题
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