|
//-----------------------------------------------------------------------
// <copyright file="MasterServices.cs" company="Self">
// Copyright (c) Md. Rashim Uddin . All rights reserved.
// </copyright>
// <author>Md. Rashim Uddin</author>
// <email>rashimiiuc@yahoo.com</email>
//-----------------------------------------------------------------------
namespace FileSystem.Replication.Server.Implementation
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using FileSystem.Replication.Server.Entities;
using FileSystem.Replication.Server.Interfaces;
/// <summary>
/// Implementaion logic for IMasterservices
/// </summary>
public class MasterServices : IMasterServices
{
#region Variables
/// <summary>
/// Contain Logger
/// </summary>
private ILogger log = null;
/// <summary>
/// Contain BackgroundWorker
/// </summary>
private BackgroundWorker backgroundWorker = null;
#endregion
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="MasterServices"/> class.
/// </summary>
/// <param name="submittedLog">The submitted log.</param>
/// <param name="submittedBackgroundWorker">The submitted background worker.</param>
public MasterServices(ILogger submittedLog, BackgroundWorker submittedBackgroundWorker)
{
this.log = submittedLog;
this.backgroundWorker = submittedBackgroundWorker;
}
#endregion
#region IMasterServices Members
/// <summary>
/// Copies the file.
/// </summary>
/// <param name="submittedEntities">The submitted entities.</param>
/// <returns>true if Succeed otherwiae false</returns>
public bool CopyFile(MyFileSystemEntities submittedEntities)
{
if (submittedEntities == null)
{
throw new ArgumentNullException();
}
if (string.IsNullOrEmpty(submittedEntities.CopyFrom))
{
throw new ArgumentNullException();
}
else if (string.IsNullOrEmpty(submittedEntities.CopyTo))
{
throw new ArgumentNullException();
}
else
{
try
{
if (File.Exists(submittedEntities.CopyFrom))
{
this.log.Log("Coping file : \n\n Source :" + submittedEntities.CopyFrom + "\n Destination :" + submittedEntities.CopyTo + "\n");
File.Copy(submittedEntities.CopyFrom, submittedEntities.CopyTo, true);
return true;
}
return false;
}
catch (Exception oEx)
{
this.log.LogM("CopyFile", oEx.ToString());
return false;
}
}
}
#endregion
#region CopyDirectory
/// <summary>
/// Copies the directory.
/// </summary>
/// <param name="submittedEntities">The submitted entities.</param>
/// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
/// <returns>return true if succeeded otherwise false</returns>
public bool CopyDirectory(MyFileSystemEntities submittedEntities, DoWorkEventArgs e)
{
if (submittedEntities == null)
{
throw new ArgumentNullException();
}
else if (string.IsNullOrEmpty(submittedEntities.CopyFrom))
{
throw new ArgumentNullException();
}
else if (string.IsNullOrEmpty(submittedEntities.CopyTo))
{
throw new ArgumentNullException();
}
else
{
try
{
DirectoryInfo sourceDir = new DirectoryInfo(submittedEntities.CopyFrom);
DirectoryInfo destDir = new DirectoryInfo(submittedEntities.CopyTo);
if (!destDir.Exists)
{
destDir.Create();
}
foreach (FileInfo childFile in sourceDir.GetFiles())
{
if (this.backgroundWorker != null)
{
if (this.backgroundWorker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
this.log.Log("Coping file : \n\n Source :" + childFile.FullName + "\n Destination :" + Path.Combine(destDir.FullName, childFile.Name) + "\n");
childFile.CopyTo(Path.Combine(destDir.FullName, childFile.Name), true);
}
}
}
foreach (DirectoryInfo subDir in sourceDir.GetDirectories())
{
if (this.backgroundWorker != null)
{
if (this.backgroundWorker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
if (!subDir.Exists)
{
this.log.Log("Directory Created :" + subDir + "\n");
subDir.Create();
}
bool retVal;
retVal = this.CopyDirectory(new MyFileSystemEntities() { CopyFrom = subDir.FullName, CopyTo = Path.Combine(destDir.FullName, subDir.Name) }, e);
if (!retVal)
{
return false;
}
}
}
}
return true;
}
catch (Exception oEx)
{
this.log.LogM("CopyDirectory", oEx.ToString());
return false;
}
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.