using System.IO;
// sShortFileName - Dateiname ohne Pfad
// sSourceDir - Quellpfad, dort, wo die Datei herstammt
// sDestDir - Zielpfad, dort, wo die Datei hin kopiert werden soll
private bool CopyFile(string sShortFileName, string sSourceDir, string sDestDir)
{
bool bRetVal = false;
int iBlockSize = 1048576; // Standard: 1MB Blockgröße
FileMode fmMode = FileMode.CreateNew;
try
{
if (File.Exists(sDestDir + sShortFileName))
{
if (MessageBox.Show("Datei existiert, überschreiben?", "Frage", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
fmMode = FileMode.Create;
}
}
// Dateigröße holen
FileInfo fi = new FileInfo(sSourceDir + sShortFileName);
// bei kleinen Dateien die Blockgröße herabsetzen
if (fi.Length < iBlockSize) iBlockSize = (int)fi.Length;
BinaryReader brRead = new BinaryReader(new FileStream(sSourceDir + sShortFileName, FileMode.Open));
// wenn die Datei nicht überschrieben werden soll (fmMode = FileMode.CreateNew)
// wird hier eine Exception geworfen, das kopieren bricht selbsständig ab
BinaryWriter brWrite = new BinaryWriter(new FileStream(sDestDir + sShortFileName, fmMode));
int iLength = (int)(fi.Length / iBlockSize);
int iRest = (int)(fi.Length % iBlockSize);
// tspbProgress ist eine ToolStripProgressbar
tspbProgress.Value = 0;
tspbProgress.Minimum = 0;
tspbProgress.Maximum = (iRest == 0) ? iLength : iLength + 1;
// 1MB-Blöcke kopieren
for (int s = 0; s < iLength; s++)
{
brWrite.Write(brRead.ReadBytes(iBlockSize));
tspbProgress.Value++;
Application.DoEvents();
}
// den Rest (< 1MB) nachkopieren
if (iRest > 0)
{
brWrite.Write(brRead.ReadBytes(iRest));
tspbProgress.Value++;
Application.DoEvents();
}
brRead.Close();
brWrite.Close();
bRetVal = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
return bRetVal;
}