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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
AlanRank #669 · 3.6K reputationPosted Jun 21, 2007, 1:19 AM EDT
Can you post some code.
Drew Taylor66 reputationPosted Jun 21, 2007, 4:01 AM EDT
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)
AlanRank #669 · 3.6K reputationPosted Jun 21, 2007, 6:04 AM EDT
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);
Posted Jun 21, 2007, 6:25 AM EDT
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