I have the full path of a file stored in a string called currFileName
string currFileName = sourceFiles[i].FullName; //Store The Dir and File Name
Is there any way to tell it to copy the file stored at this location??
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
DanPosted Nov 25, 2007, 7:33 PM
Could somebody tell me what goes inside the background worker??
Does the background worker contain the code for actually copying the files, or is this still done by the main form?
I would assume that it is like this:
- The main form will still do all the transferring/copying of files...
- The background worker will sit within the main form
- The background worker will calculate what % of the work has been done and then report this to the progress bar on the second form and update the progress bar .Value() property.
- This will be done using .reportprogress()
- WorkReportsProgress will be set to 'true'
Hope that makes sense.
DanPosted Nov 25, 2007, 5:50 PM
I have fixed the problem below. I replaced .Replace() with
.CopyTo() and instructed it to overwrite an exisiting file.
I didn't realise that replace deletes the original file and there is no choice about this.
------------------------------------------------------
After further testing I have a problem
//Replace The File
sourceFiles[i].Replace(newFileFullName,null); //Replace the file with the source file
}
This line seems to correctly replace the file in the DESTINATION folder as expected.
However it also DELETES the file in the SOURCE folder, which I do not want to happen.
When using .Replace() you can not just put in the name of the file that will be replaced, you need to put something where I typed "null".
DanPosted Nov 25, 2007, 5:39 PM
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace BackupUtility
{
public partial class Form1 : Form
{
//public variable declarations
string sourceDir;
string destDir;
public Form1()
{
InitializeComponent();
}
private void helpFileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backupData(string source, string destination)
{
}
private void cmdBrowseSource_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbdSource = new FolderBrowserDialog();
fbdSource.ShowNewFolderButton = false;
DialogResult source = fbdSource.ShowDialog();
if (source == DialogResult.OK)
{
txtSource.Text = fbdSource.SelectedPath;
}
sourceDir = txtSource.Text;
}
private void cmdBrowseDest_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbdDest = new FolderBrowserDialog();
fbdDest.ShowNewFolderButton = false;
DialogResult source = fbdDest.ShowDialog();
if (source == DialogResult.OK)
{
txtDest.Text = fbdDest.SelectedPath;
}
destDir = txtDest.Text;
}
private void cmdBackup_Click(object sender, EventArgs e)
{
DirectoryInfo diSourceDir = new DirectoryInfo(sourceDir);
DirectoryInfo diDestDir = new DirectoryInfo(destDir);
FileInfo[] sourceFiles = diSourceDir.GetFiles();
FileInfo[] destFiles = diDestDir.GetFiles();
int sourceFilesCount = sourceFiles.GetLength(0);
int destFilesCount = destFiles.GetLength(0);
int matchCount = 0; //Used to store number of matches found
for (int i = 0; i < sourceFilesCount; i++)
{
string currFileName = sourceFiles[i].Name; //Store Name of File
string currFileFullName = sourceFiles[i].FullName; //Store The Dir and File Name
string newFileFullName = destDir + "\\" + currFileName; // Store intended path for new file
bool matchFound = checkFiles(currFileName, destDir); //Check if a match is found
if (matchFound == false) //If no match is found copy files
{
sourceFiles[i].CopyTo(newFileFullName); //Copy the file with " BACKUP" on the end
lblStatus.Text = "Copying: " + currFileName; //Output Message to User via Label
}
else //If a match is found check is source file newer?
{
bool sourceNewer = checkSourceFileNewer(currFileFullName, newFileFullName);
if (sourceNewer == true) //IF the source file is newer than the destination file
{
//Replace The File
sourceFiles[i].Replace(newFileFullName,null); //Replace the file with the Source File
lblStatus.Text = "Copying: " + currFileName; //Output Message to User via Label
}
else //If the source is NOT newer than the destination file
{
matchCount++;//Add to counter of files not copied
}
}
}
if (matchCount > 0)
{
MessageBox.Show(matchCount + " files were not copied - Newer versions already exist");
}
}
private bool checkFiles(string fileToCopy, string destLocation)
{
string checkString = destLocation + "\\" + fileToCopy; //Store the Location to Check
FileInfo checkFile = new FileInfo(checkString); //Create checkFile FileInfo
//Check to see if a match exists and return result
if (checkFile.Exists == true)
{
return true;
}
else
{
return false;
}
}
private bool checkSourceFileNewer(string sourceFile, string destFile)
{
FileInfo sourceFilePath = new FileInfo(sourceFile); //Create source File FileInfo
FileInfo destFilePath = new FileInfo(destFile); // Create dest File FileInfo
//IF source file is newer than dest file return true
if (sourceFilePath.LastWriteTime > destFilePath.LastWriteTime)
{
return true;
}
else //IF source file is older than dest file return false
{
return false;
}
}
}
}
DanPosted Nov 25, 2007, 5:39 PM
I have now progressed with the code. If a matching file is found in the destination directory it now checks to see which one was edited last and if the source file is newer it replaces the file in the destination folder, if the source file is older it just adds a 1 to a counter of files that were not copied.
I am wondering if anybody can advise me on other things I should check before overwriting the file. Is checking that the source file was written to since the last copy enough, or should I compare anything else before giving the go-ahead to copy the files??
The other thing I wanted to know is how can I go about giving the user some feedback on what is happening. For exmaple if they were to copy some larger files how can I get a progress bar etc to appear?
Is there some sort of progress dialogue built into .NET that looks like what you see when copying large files in windows? I am going to look for this now but if anybody can let me know what to be looking at thats great.
Following post contains code so far, if you have any comments then please let me know.
AlanPosted Nov 25, 2007, 11:25 AM
Hi Dan,
I'd suggest changing this line:
sourceFiles[i].CopyTo(currFileName + " BACKUP"); //Copy the file with " BACKUP" on the end
to this;
sourceFiles[i].CopyTo(currFileFullName + " BACKUP"); //Copy the file with " BACKUP" on the end
Otherwise, the file will be copied to the current working directory rather than the destination directory chosen by the user.
DanPosted Nov 25, 2007, 9:31 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace BackupUtility
{
public partial class Form1 : Form
{
//public variable declarations
string sourceDir;
string destDir;
public Form1()
{
InitializeComponent();
}
private void helpFileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backupData(string source, string destination)
{
}
private void cmdBrowseSource_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbdSource = new FolderBrowserDialog();
fbdSource.ShowNewFolderButton = false;
DialogResult source = fbdSource.ShowDialog();
if (source == DialogResult.OK)
{
txtSource.Text = fbdSource.SelectedPath;
}
sourceDir = txtSource.Text;
}
private void cmdBrowseDest_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbdDest = new FolderBrowserDialog();
fbdDest.ShowNewFolderButton = false;
DialogResult source = fbdDest.ShowDialog();
if (source == DialogResult.OK)
{
txtDest.Text = fbdDest.SelectedPath;
}
destDir = txtDest.Text;
}
private void cmdBackup_Click(object sender, EventArgs e)
{
DirectoryInfo diSourceDir = new DirectoryInfo(sourceDir);
DirectoryInfo diDestDir = new DirectoryInfo(destDir);
FileInfo[] sourceFiles = diSourceDir.GetFiles();
FileInfo[] destFiles = diDestDir.GetFiles();
int sourceFilesCount = sourceFiles.GetLength(0);
int destFilesCount = destFiles.GetLength(0);
for (int i = 0; i < sourceFilesCount; i++)
{
string currFileName = sourceFiles[i].Name; //Store Name of File
string currFileFullName = sourceFiles[i].FullName; //Store The Dir and File Name
sourceFiles[i].CopyTo(currFileName + " BACKUP"); //Copy the file with " BACKUP" on the end
string newFile = currFileFullName + " BACKUP"; // Store the name of the new file that was created
//TODO:Move the "newFile" to folder stored in destDir
}
}
}
}
AlanPosted Nov 24, 2007, 6:07 PM
DanPosted Nov 24, 2007, 5:31 PM
This however does not work how I planned above. Could somebody offer some guidance, without giving me the actual answer, as I'd like to figure as much of it out as possible.