Introduction
I just became engrossed with the DotNetNuke Framework and have made a number of new modules to
try out, but when it came time to upload the app -- provided I uploaded it all, it was over 60 Megs. That's
a lot of time and transfer just for a bunch of unnecessary files. The best advice I had to strip out the
unwanted files was to Search for them (in a copy) and delete them -- ONE PASS FOR EACH FILE. This was not
going to do.
I did find another app that did what this one does with some extra features for zipping and ftping, but I
was just looking for something to copy only the selected files and the existing folder structure to a new folder
and I could deal with getting them up on the server myself.
The only snag in my plan was I didn't know exactly what file types were needed to fully support a DNN site.
So I made the app scan the source folder for all the possible file extensions and then allow the user to uncheck
the ones that shouldn't make the trip.
I didn't see anything else (besides the one mentioned earlier) that did this, so I thought I'd post it here
for all to download.
Using the code
This is a very small little app -- only 7 methods. Let's get started.
Folder Browsing
First I needed a FolderBrowser
Dialog (something I sorely missed with .net 1.0). Note the use of the Description
attribute. Give the user something to read on the box.
private void btnSource_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.Description =
"Choose the Source Location to copy files from.";
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
tboxSource.Text = folderBrowserDialog1.SelectedPath;
}
}
private void btnDestination_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.Description =
"Choose the Destination Location to copy files to.";
DialogResult result = folderBrowserDialog1.ShowDialog();
if( result == DialogResult.OK )
{
tboxDestination.Text = folderBrowserDialog1.SelectedPath;
}
}
I use a couple of textboxes to store the result -- or allow the user to just type them in.
Folder Checking
This routine, given a source and destination DirectoryInfo
objects, iterates through the subfolders
in the source and reiteratively calls itself to repeat the process, either making copies of all the folders and their subfolders
if the site is being copied, or just allows the CheckFiles
method to scan the files, but I get ahead of myself.
private void CheckDirectories(DirectoryInfo source,
DirectoryInfo destination, bool Deploy)
{
foreach (DirectoryInfo tempdir in source.GetDirectories())
{
if (Deploy)
{
destination.CreateSubdirectory(tempdir.Name);
tboxCopiedFiles.Text += "Creating Folder " +
destination.FullName + "\\" + tempdir.Name + "\r\n";
progressBar1.Increment(1);
}
//
DirectoryInfo tSource = new DirectoryInfo(
source.FullName + "\\" + tempdir.Name);
DirectoryInfo tDestin = new DirectoryInfo(
destination.FullName + "\\" + tempdir.Name);
CheckDirectories(tSource, tDestin, Deploy);
folderCount++;
CheckFiles(tSource, tDestin, Deploy);
}
}
File Checking
This next section either loads up the lboxExtensions
listbox or actually does the file copying. It's simply
a matter of making a FileInfo
object for each thing the the directory and parsing out the extension or copying it
to the destination.
private void CheckFiles(DirectoryInfo source,
DirectoryInfo destination, bool Deploy)
{
string extension = "";
foreach (FileInfo tempfile in source.GetFiles())
{
extension = tempfile.Extension.ToLower();
if (Deploy)
{
foreach (string item in lboxExtensions.CheckedItems)
{
if (item == extension)
{
tempfile.CopyTo(destination.FullName + "\\" + tempfile.Name, true);
fileCount++;
tboxCopiedFiles.Text += "Copying file " + tempfile.Name + "\r\n";
}
}
}
else
{
if (!lboxExtensions.Items.Contains(extension))
lboxExtensions.Items.Add(extension, true);
}
}
}
Note:
I reused the Directory/File checking methods for both the scanning and the copying. I pass it the boolean mDeploy
to control their behavior.
Button Clicking
I probably could have wrapped these into one EventHandler
but -- I didn't do it. Sorry. A victim of RAD.
Pretty straight forward code here. Just a call to start the process (next -- finally!) and for working
the ProgressBar
.
private void btnDeploy_Click(object sender, System.EventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = folderCount;
this.StartProcessing(true);
tboxCopiedFiles.Text += "Folders created: " +
folderCount.ToString() + "\r\n";
tboxCopiedFiles.Text += "Files copied: " + fileCount.ToString();
progressBar1.Value = 0;
}
private void btnScan_Click(object sender, System.EventArgs e)
{
this.StartProcessing(false);
lboxExtensions.Sorted = true;
this.btnDeploy.Enabled = true;
}
The Starting Point
I made this routine to be outside the reiterative process as a place to either start the Scan or the Deployment.
It sets the button Enabling and kicks it all off. It also allows for one place for the try/catch block to be set.
This method accepts the boolean mDeploy
and passes that to the other methods.
private void StartProcessing(bool mDeploy)
{
this.btnDeploy.Enabled = false;
this.btnScan.Enabled = false;
tboxCopiedFiles.Text = "";
try
{
DirectoryInfo srce = new DirectoryInfo(tboxSource.Text);
DirectoryInfo dest = new DirectoryInfo(tboxDestination.Text);
CheckDirectories(srce, dest, mDeploy);
CheckFiles(srce, dest, mDeploy
}
catch (Exception ex)
{
MessageBox.Show("I/O Error", "Did you choose your folders?",
MessageBoxButtons.OK);
}
if (this.lboxExtensions.Items.Count > 0)
this.btnDeploy.Enabled = true;
this.btnScan.Enabled = true;
}
Points of Interest
All in all a quick and easy project. Allowing me not to move fewer files up -- though I would still like to know
exactly what files are needed. Emails on that subject are welcomed. This could easily be employed for a non-Website
publishing purposes, but that's what I needed it for.
History
- Version 1.0 completed : 8.9.2004