Tuesday, October 1, 2013

Send Folder from server to Client

1- I'll suppose that you have a method called 'SendFile()' takes the file name and send it to the server.



/// <summary>
/// the already existing method to send files.
/// </summary>
/// <param name="FileName">the file name</param>
public void SendFile(string FileName)
{
// Code to send a single file.
}

2- I created an overloaded function takes the file path and directory. this method will copy the file to a destination folder, and it'll create it if it doesn't exist.



/// <summary>
/// Create a directory and copy the file
/// </summary>
/// <param name="SourceFilePath">the full Path of the file to copy</param>
/// <param name="DestinationDirectoryName">destination directory\subdirectory name</param>
public void SendFile(string SourceFilePath, string DestinationDirectoryName)
{
// Code to Create Create Directory
if (!Directory.Exists(DestinationDirectoryPath + @"\" + DestinationDirectoryName))
Directory.CreateDirectory(DestinationDirectoryPath + @"\" + DestinationDirectoryName);

string FileName = SourceFilePath.Split('\\')[SourceFilePath.Split('\\').Length - 1];

// as an example I copy the files to the new destination.
File.Copy(SourceFilePath, DestinationDirectoryPath + @"\" + DestinationDirectoryName + @"\" + FileName);
}

3- I created a method called 'SendDirectory' to copy the files and directories to the destination location.



/// <summary>
/// Sends a directory with all files and subdirectories.
/// </summary>
/// <param name="inDirectoryPath">Directory to copy</param>
public void SendDirectory(string inDirectoryPath)
{
string DirectoryName = inDirectoryPath.Substring(inDirectoryPath.IndexOf(SourceDirectoryName), inDirectoryPath.Length - inDirectoryPath.IndexOf(SourceDirectoryName));
string[] files = Directory.GetFiles(inDirectoryPath);
foreach (string file in files)
{
// Call Send File with the file full path and destination Directory Name
SendFile(file, DirectoryName);
}
string[] directories = Directory.GetDirectories(inDirectoryPath);
foreach (string directory in directories)
{
// Calls itself for each subdirectory.
SendDirectory(directory);
}
}

4- Here is a final working code to copy the files to a directory, you can change the part of coping the files and creating the folder with code to perform it on the server or client.



string SourceDirectoryName;
string DestinationDirectoryPath;
string SourceDirectoryPath;

/// <summary>
/// Sends a directory with all files and subdirectories.
/// </summary>
/// <param name="inDirectoryPath">Directory to copy</param>
public void SendDirectory(string inDirectoryPath)
{
string DirectoryName = inDirectoryPath.Substring(inDirectoryPath.IndexOf(SourceDirectoryName), inDirectoryPath.Length - inDirectoryPath.IndexOf(SourceDirectoryName));
string[] files = Directory.GetFiles(inDirectoryPath);
foreach (string file in files)
{
// Call Send File with the file full path and destination Directory Name
SendFile(file, DirectoryName);
}
string[] directories = Directory.GetDirectories(inDirectoryPath);
foreach (string directory in directories)
{
// Calls itself for each subdirectory.
SendDirectory(directory);
}
}
/// <summary>
/// the already existing method to send files.
/// </summary>
/// <param name="FileName">the file name</param>
public void SendFile(string FileName)
{
// Code to send a single file.
}

/// <summary>
/// Create a directory and copy the file
/// </summary>
/// <param name="SourceFilePath">the full Path of the file to copy</param>
/// <param name="DestinationDirectoryName">destination directory\subdirectory name</param>
public void SendFile(string SourceFilePath, string DestinationDirectoryName)
{
// Code to Create Create Directory
if (!Directory.Exists(DestinationDirectoryPath + @"\" + DestinationDirectoryName))
Directory.CreateDirectory(DestinationDirectoryPath + @"\" + DestinationDirectoryName);

string FileName = SourceFilePath.Split('\\')[SourceFilePath.Split('\\').Length - 1];

// as an example I copy the files to the new destination.
File.Copy(SourceFilePath, DestinationDirectoryPath + @"\" + DestinationDirectoryName + @"\" + FileName);
}

private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
SourceDirectoryName = folderBrowserDialog1.SelectedPath.Split('\\')[folderBrowserDialog1.SelectedPath.Split('\\').Length - 1];
SourceDirectoryPath = folderBrowserDialog1.SelectedPath;
}

private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
DestinationDirectoryPath = folderBrowserDialog1.SelectedPath;
SendDirectory(SourceDirectoryPath);
}

I hope it helps.



Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


No comments:

Post a Comment