cmullins
Sun Aug 14 12:01:01 CDT 2005
Jon,
Sure thing. Here is the code I was using. If the code completes the
finally block is called correctly. It is only when the console is closed
while files are being copied.
using System;
using System.Collections;
using System.Text;
using System.IO;
namespace BackupComputerConsole
{
class StartBackup
{
FileStream fs;
System.IO.StreamWriter sw;
DateTime lastBackupDateTimeValue;
DateTime thisBackupDateTime;
const string BackupLastDateTimeFileName = @"c:\backupLastDateTime.txt";
const string BackupLogFileName = @"c:\backuplog.txt";
static void Main(string[] args)
{
// Ensure the parameters are passed
if(args.Length!=3)
{
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Usage: ");
Console.WriteLine("");
Console.WriteLine("StartBackup.exe <string: Directory to copy> <string:
Destination Directory> <bool: Overwrite files>");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Example: ");
Console.WriteLine("");
Console.WriteLine(@"StartBackup.exe c:\ x:\ true");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
else
{
new StartBackup().OnStart(args[0], args[1], Boolean.Parse(args[2]));
}
}
#region Private Properties
private DateTime LastBackupDateTime
{
get
{
if (this.lastBackupDateTimeValue == DateTime.MinValue)
{
try
{
FileStream fsGetLastBackupDateTime = new
FileStream(@"c:\backupLastDateTime.txt", FileMode.Open);
StreamReader sr = new StreamReader(fsGetLastBackupDateTime);
this.lastBackupDateTimeValue = DateTime.Parse(sr.ReadLine());
sr.Close();
}
catch
{
this.lastBackupDateTimeValue = DateTime.MinValue;
}
}
return this.lastBackupDateTimeValue;
}
}
#endregion
#region Private Methods
private string CheckAndCreateDirectory(string directoryName)
{
if (directoryName.Substring(3).Length > 0)
{
// Append the directory with a slash
if (!directoryName.EndsWith(@"\"))
{
directoryName += @"\";
}
// Create the directory if it doesn't exist
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
}
return directoryName;
}
private void UpdateLastBackup()
{
FileStream fsUpdateBackupDateTime = new
FileStream(BackupLastDateTimeFileName, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fsUpdateBackupDateTime);
sw.Write(this.thisBackupDateTime);
sw.Flush();
sw.Close();
}
private void OnStart(string directoryToCopy, string directoryDestination,
bool overWriteFiles)
{
try
{
this.thisBackupDateTime = DateTime.Now;
fs = new FileStream(BackupLogFileName, FileMode.OpenOrCreate);
sw = new StreamWriter(fs);
//Create a Directory object using DirectoryInfo
DirectoryInfo directoryToCopyInfo = new DirectoryInfo(directoryToCopy);
//Pass the Directory for displaying the contents
getDirsFiles(directoryToCopyInfo, directoryDestination, overWriteFiles);
}
finally
{
// Update Backup Time
this.UpdateLastBackup();
sw.Flush();
sw.Close();
fs.Close();
}
}
private void getDirsFiles(DirectoryInfo singledirectory, string
directoryDestination, bool overWriteFiles)
{
//create an array of files using FileInfo object
FileInfo[] files;
try
{
//get all files for the current directory
files = singledirectory.GetFiles();
//iterate through the directory and print the files
foreach (FileInfo file in files)
{
try
{
this.CopyFile(file.FullName, directoryDestination + @"\" +
singledirectory.FullName.Substring(3), overWriteFiles);
}
catch (Exception subdirectoryFileCopyException)
{
sw.WriteLine("Problem with copying file " + file.FullName + "." +
Environment.NewLine + "Error: " + subdirectoryFileCopyException.StackTrace +
Environment.NewLine + subdirectoryFileCopyException.Message);
sw.WriteLine("");
continue;
}
}
}
catch (Exception e)
{
sw.WriteLine("Problem with " + singledirectory + ". " +
Environment.NewLine + "Error: " + e.StackTrace + Environment.NewLine +
e.Message);
sw.WriteLine("");
}
try
{
//get sub-folders for the current directory
DirectoryInfo[] dirs = singledirectory.GetDirectories();
//This is the code that calls
//the getDirsFiles (calls itself recursively)
//This is also the stopping point
//(End Condition) for this recursion function
//as it loops through until
//reaches the child folder and then stops.
foreach (DirectoryInfo dir in dirs)
{
getDirsFiles(dir, directoryDestination, overWriteFiles);
}
}
catch (Exception e)
{
sw.WriteLine("Problem reading next directories. " + Environment.NewLine
+ "Error: " + e.StackTrace + Environment.NewLine + e.Message);
sw.WriteLine("");
}
}
private void CopyFile(string sourceFileAndDirectory, string
destinationDirectoryName, bool overwrite)
{
try
{
FileInfo sourceFileInfo = new FileInfo(sourceFileAndDirectory);
// Only copy files that have been modified since the last backup
if (sourceFileInfo.LastWriteTime > this.LastBackupDateTime)
{
// NOte, Future versions should probably pass this in as a parameter or
be set from a configuration file
Console.WriteLine("Copying " + sourceFileAndDirectory);
string destination =
(this.CheckAndCreateDirectory(destinationDirectoryName) + new
FileInfo(sourceFileAndDirectory).Name);
File.Copy(sourceFileAndDirectory, destination, overwrite);
}
else
{
// Note, uncommenting this line shows files that were skipped, but
slows down the overall copy process
// Future versions should probably pass this in as a parameter or be
set from a configuration file
//Console.WriteLine("Skipping " + sourceFileAndDirectory);
}
}
catch (IOException io)
{
sw.WriteLine("Problem with " + sourceFileAndDirectory + ". " +
Environment.NewLine + "Error: " + io.StackTrace + Environment.NewLine +
io.Message);
sw.WriteLine("");
}
}
#endregion
}
}
Thanks again,
cmullins
"Jon Skeet [C# MVP]" wrote:
> cmullins <cmullins@discussions.microsoft.com> wrote:
> > Actually, there is no exception being generated. So, the OOM exception
> > isn't coming into play.
>
> In that case, presumably the code you posted isn't actually the code in
> question, which makes it much harder to work out what's actually going
> on.
>
> Are you sure that the finally block itself isn't generating an
> exception, by the way? Have you tried putting something which is bound
> to work (and let you know that it's executed) as the first bit of the
> finally block?
>
> Could you post a short but complete program which demonstrates the
> problem? A console application would be much easier to work with than
> an ASP.NET app.
>
> See
http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>