When I copy a file the copied file has no data in it, and the file I copied it from doesn't have any data in it after the file has been copied, why is this and how can I copy the data?
Loading
When I copy a file the copied file has no data in it, and the file I copied it from doesn't have any data in it after the file has been copied, why is this and how can I copy the data?
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.
Posted Jun 21, 2007, 10:25 AM
string strFileToCopy = Convert.ToString(listBox1.SelectedItem);
if (!File.Exists(strFileToCopy))
{
MessageBox.Show("File does not exist.", "Error Help", MessageBoxButtons.OK);
}
else
{
try
{
File.Copy(strFileToCopy, "newFile.txt");
}
catch
{
MessageBox.Show("Could not copy.", "Error Help", MessageBoxButtons.OK);
}
}
Youll have to put your own file paths in and maybe make a few littls adjustments, but thats the basic jist of the code :)
Brandon
AlanPosted Jun 21, 2007, 10:04 AM
To move the file you can do something like this:
using System.IO;
.......
string fileToMove = listBox1.SelectedItem.ToString();
string path = @"c:\somefolder\" + fileToMove;
string path2 = @"c:\someotherfolder\ + fileToMove;
if (File.Exists(path2))
File.Delete(path2);
File.Move(path, path2);
Drew TaylorPosted Jun 21, 2007, 8:01 AM
It's ok I have worked around it, however, I now want to move a file from a folder to another folder (and so it keeps its data! why wouldn't it be copied any way?) How can I do this?
At the moment, I have a listbox which displays files, I want the selected file to be moved from the folder it is in to another folder (which I have specified in the code)
AlanPosted Jun 21, 2007, 5:19 AM
Can you post some code.