locked
File.Copy () to another folder and delete the original file issue RRS feed

  • Question

  • User-932240137 posted

    I am having issue with deleting  file  after the file is being copied to location B

    error message says : The process cannot access the file 'C:\demo\test.xlsx' because it is being used by another process.

      public static void ProcessFilestoBeArchive()
            {
                DataTable totalFilesToArchived = Helper.GetDocumentstoBeArchived();
                if (totalFilesToArchived.Rows.Count > 0)
                {
                    foreach (DataRow row in totalFilesToArchived.Rows)
                    {
    
                        int id = Convert.ToInt32(row["DocumentId"]);
                        string documentName = row["DocumentName"].ToString();
                        string documentPath = row["DocumentPath"].ToString();
                        int status = Convert.ToInt32(row["Status"]);
                        int clientId = Convert.ToInt32(row["ClientID"]);
                        bool Archived = Convert.ToBoolean(row["Archived"].ToString());
                        string extension = Path.GetExtension(documentName);
                        string archivedFileName = string.Format("{0}_{1}{2}", documentName, Guid.NewGuid(), extension);
                        string archivedPath = Path.Combine(archivePath, archivedFileName);
                        if (status == 3 && Archived == false)
                        {
                            try
                            {
                                File.Copy(documentPath, archivedPath, true);
                                Helper.DeleteFile(documentPath);  //  i got error here  :
    							// The process cannot access the file 'C:\demo\test.xlsx' because it is being used by another process.							
    						}
                            catch (Exception ex)
                            {  throw;
                            }
                        }                
                    }
                }
            }
    Monday, December 4, 2017 2:04 AM

All replies

  • User-189443255 posted

    You can just use File.Move

    Monday, December 4, 2017 2:41 AM
  • User-932240137 posted
    File.Move(documentPath, archivedPath); The process cannot access the file because it is being used by another process.

    Monday, December 4, 2017 2:47 AM
  • User-932240137 posted

    Can anyone help ? I can copy file to another location without any issue but could not Delete the file :)

    Monday, December 4, 2017 5:04 AM
  • User991499041 posted

    Hi Boracay,

    Have you closed the write stream when you create the file?

    You need to close the stream then delete.

    You can also wrap all your code inside a using statement:

    This pattern will ensure that the file won't be left open in case of exceptions, it may be the reason the file is in use.

    You can refer this answer.

    Regards,

    zxj

    Monday, December 4, 2017 8:00 AM
  • User347430248 posted

    Hi Boracay,

    you did not posted , how did you created an object of Helper?

    why you did not directly use File.delete?

    below code is working fine on my side.

     protected void Page_Load(object sender, EventArgs e)
            {
                
                string path = @"C:\Users\v-padee\Desktop\demo_text.txt";
                FileInfo file = new FileInfo(path);
                if (file.Exists)//check file exsit or not
                {
                    file.Delete();
                    Label1.Text = "File deleted Successfully...";
                }
                else
                {
                    Label1.Text = "File not deleted...";
                }
            }

    you can try to make a test on your side and let us know about the result.

    we will try to provide you further suggestions to solve the issue.

    Regards

    Deepak

    Monday, December 4, 2017 8:03 AM