I want to be able to allow a user to select a file in a listbox which displays files from a location they have specified, they then click copy which makes a copy of the file they have selected, then I want to to allow the user rename the copied file and then have the file sent to a specified folder.
I have tried to make this work but I'm not sure what the best way to do it is? (also not sure of the code needed)
Currently I have a browse button which allows the user to select a location to read the files from, which then populates a textbox they then click open and a listbox is populated with the files. The user then selects/Highlights a file in the listbox and clicks the copy button, this copies the selected file and adds copied to the end of the file - how can I allow the user to rename the copied file? Is a better way to allow them to have one button that does all of this?
If you think you know how to do this, please help!
Loading
Jan MontanoPosted Jun 21, 2007, 10:24 PM
private void btnBrowse_Click(object sender, EventArgs e)
{
const string fileFormat = "*.*";
string filePath;
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK) // user selected a file in the dialog then pressed ok
{
filePath = folderBrowserDialog1.SelectedPath;
txtFilePath.Text = filePath;
DirectoryInfo dInfo = new DirectoryInfo(filePath);
FileInfo[] fInfo = dInfo.GetFiles(fileFormat);
this.filesListBox.Items.Clear();
foreach (FileInfo FI in fInfo)
{
filesListBox.Items.Add(FI);
}
}
}
// my suggestion would be to have a txtDestinationFile where user can enter or replace the filename if he wants to. It's up to you if you want a fileBrowserDialog for the new path and new filename.
private void btnCopy_Click(object sender, EventArgs e)
{
string sourceFile = "";
string destinationFile = txtDestinationFileName.Text;
destinationFile = @"C:\MyNewFile"; // for testing purposes
if (filesListBox.SelectedIndex >= 0) // user selected an item
{
sourceFile = filesListBox.SelectedItem.ToString();
File.Copy(Path.Combine(txtFilePath.Text,sourceFile), destinationFile, false);
}
}
Drew TaylorPosted Jun 21, 2007, 4:25 AM
I tried doing it a different way but it didn't work, I don't understand why I'm having so much trouble with this I have been able to work out how to do other stuff :(
I don't get what it means here in the msdn definition for copy.move,
I have tried doing
string path = selectedFile;
string path2 = path + "temp"
what is the code actually doing here? Am I missing something? All I want the code to do is copy a file to another folder called something like copiedfiles and then allow the user to rename the copied file.
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";
try
{
// Create the file and clean up handles.
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed.
File.Copy(path, path2, true);
Console.WriteLine("The second Copy operation succeeded, which was expected.");
}
catch
{
Console.WriteLine("Double copy is not allowed, which was not expected.");
}
Drew TaylorPosted Jun 21, 2007, 3:59 AM
Thanks for taking the time to explain this. I copied and pasted the parts of the code to fit in with my program but get this error:
Additional information: The path is not of a legal form.
On this line when I click the Open button,
string[] fileEntries = Directory.GetFiles(this.folderBrowserDialog1.SelectedPath);
how can I change this to specify a location such as G:\ ?
Wim DevosPosted Jun 20, 2007, 12:43 PM
It is not exactly what you need but you should be able to convert it...
Drew TaylorPosted Jun 20, 2007, 10:32 AM
Thanks it looks like you know your stuff, but that seems more difficult to do for some reason. I think I'll try and do it the way I was trying to do before.
I have the following code:
A button to get the files from the specified location and put them in the list box called filesListBox
DirectoryInfo dInfo =
new DirectoryInfo(FilePath);FileInfo[] fInfo = dInfo.GetFiles("*.");
this.filesListBox.Items.Clear(); foreach (FileInfo FI in fInfo){
filesListBox.Items.Add(FI);
textBox1.Text=(DateTime.Now.ToShortTimeString() + @"Template Mark Files from G:\Template Markdown Files");
}
this.button1.Enabled = false;From this point, how can I?
Step 1) Allow the user to select a file in the filesListBox, click a button called copy which copies the file to a directory
Step 2) Once the file has been copied, the copied file name is displayed in a box which the user can use to rename the file, when they press update file name button it changes the file name to the name they entered
Step 3) The user clicks a button and the newly copied and renamed file is sent to a directory
Can you help with this?
Wim DevosPosted Jun 20, 2007, 8:18 AM
private void btnOpen_Click(object sender, EventArgs e) { this.lbFiles.Items.Clear(); if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string[] fileEntries = Directory.GetFiles(this.folderBrowserDialog1.SelectedPath); this.lbFiles.Items.AddRange(fileEntries); } } private void btnCopy_Click(object sender, EventArgs e) { string selectedFile = this.lbFiles.SelectedItem.ToString(); FileStream fin = new FileStream(selectedFile,FileMode.Open); if(this.saveFileDialog1.ShowDialog() == DialogResult.OK) { FileStream fout = new FileStream(this.saveFileDialog1.FileName, FileMode.Create); try { int i; do { i = fin.ReadByte(); if (i != -1) fout.WriteByte((byte)i); } while (i != -1); fout.Close(); } catch (IOException exc) { } } fin.Close(); }Drew TaylorPosted Jun 20, 2007, 7:40 AM
Thats a good idea! :-) But I don't know how to do that, I could probably find out how to do the save dialot part but the reason I am asking in this forum is because I have tried a few times to get this to work but can't, and was hoping someone could provide me with some code?
Wim DevosPosted Jun 20, 2007, 6:48 AM