2

I am trying to to delete file after successful copy. I want the original file to be deleted after I copied it.

private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "All Files(*.*)|*.*";
    if (open.ShowDialog() == DialogResult.OK) 
    {
        string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + id.ToString()+Path.GetExtension(open.FileName);
        if (!Directory.Exists(Application.StartupPath + "\\AttachedFiles"))
        {
            Directory.CreateDirectory(Application.StartupPath + "\\AttachedFiles");
        }
        File.Copy(open.FileName, Path.Combine(Application.StartupPath + "\\AttachedFiles", filename));
        cnx.ExecuteCmd("insert into Attachement values('" + id + "','" + filename + "','" + Path.GetFileName(open.FileName) + "')");
        MessageBox.Show("attached success");
        listBox1.DataSource = cnx.SelectCmd("select * from Attachement where Accidentid='" + id + "'");
        listBox1.DisplayMember = "RealFilename";
        listBox1.ValueMember = "Filename";
    }
}
4
  • 9
    Why are you using Copy and not Move? Apr 5, 2019 at 7:54
  • i try move but its not working right with me
    – binkiteb
    Apr 5, 2019 at 7:57
  • 1
    "i try move but its not working right with me" - what do you mean by that? Do you get an exception - if so, please give details.
    – PaulF
    Apr 5, 2019 at 8:00
  • 2
    @binkiteb Can you explain how it isn't working? Does it throw an exception (what exception?)? It almost sounds like you don't have delete permissions on the source file, or the source file is in use. If either of these are the case, then you don't have any more luck with Copy/Delete in terms of removing the original. Apr 5, 2019 at 8:00

1 Answer 1

4

To delete a File you can use

File.Delete(filePath)

But why don't move it with a single command instead?

File.Move(filePathSource, filePathDestination);

If you can't delete or move a file, you probably still have a Stream open.

Here a working example, how to use an OpenFileDialog and delete and copy the selected file.

using (File.Create(@"c:\Temp\txt.txt")); // File.Create wrapped in a using() to ensure disposing the stream.

using (OpenFileDialog ofd = new OpenFileDialog())
{
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        File.Copy(ofd.FileName, ofd.FileName + "2.txt");
        File.Delete(ofd.FileName);
        File.Delete(ofd.FileName + "2.txt");
    }
}

Notice, that i wrap a using(...) around the File.Create(). This is because it opens a Stream to the File, which locks it. If you remove the using(...) around the File.Create() the deletion will not work.

To understand why you can't delete your File, you have to search your code for any access to the file.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.