none
The process cannot access the file because it is being used by another process. RRS feed

  • Question

  • Hi,

    i am developing console application which read excel file save processing data into text file, application work file when i publish and create setup file, while running setup/installation of application getting following error:

    following is code:

                                       

    public void DeleteProductsListItems()
            {
                try
                {
                    if (!System.IO.File.Exists($"E:\\MyLogFile\\" + "log_"+DateTime.Now.ToString("MM_dd_yyyy")+ ".txt"))
                    {
                        var myFile = System.IO.File.Create($"E:\\MyLogFile\\" + "log_"+DateTime.Now.ToString("MM_dd_yyyy")+ ".txt");
                        myFile.Close();
                        Thread.Sleep(1000);

                    }
                    using (var stream = System.IO.File.Open($"E:\\MyLogFile\\" + "log_" + DateTime.Now.ToString("MM_dd_yyyy") + ".txt", FileMode.Open, FileAccess.Write, FileShare.Read))
                    {
                        System.IO.File.AppendAllText($"E:\\MyLogFile\\" + "log_" + DateTime.Now.ToString("MM_dd_yyyy") + ".txt", "Start deleting Products SP List: Date:" + DateTime.Now.ToLongDateString() + "||" + DateTime.Now.ToLongTimeString() + "" + Environment.NewLine);

                    }



                    System.IO.File.AppendAllText($"E:\\MyLogFile\\" + "log_"+DateTime.Now.ToString("MM_dd_yyyy")+ ".txt", "Deletion has been : Date:" + DateTime.Now.ToLongDateString() + "||" + DateTime.Now.ToLongTimeString() + "" + Environment.NewLine);


                }
                catch (Exception ex)
                {
                    System.IO.File.AppendAllText($"E:\\MyLogFile\\" + "log_"+DateTime.Now.ToString("MM_dd_yyyy") + ".txt", "Error  Has been occurs while deleting Product List:" + ex + "" + Environment.NewLine);

                }
            }

    pls guide what could be an issue and how to fix it.Thx


    iffi

    Wednesday, April 17, 2019 5:30 AM

All replies

  • Hi

    Thank you for posting here.

    Based on your description, you want to solve the exception that you got.

    I have reproduced your problem, you could try to delete the following code.

      using (var stream = System.IO.File.Open($"D:\\MyLogFile\\" + "log_" + DateTime.Now.ToString("MM_dd_yyyy") + ".txt", FileMode.Open, FileAccess.Write, FileShare.Read))
                    {
    
                    }
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 17, 2019 7:27 AM
    Moderator
  • To clarify what Jack is saying, Your using statement is opening the file for writing with no one else being able to write at the same time. You then call the File.Append method which tries to open the file for writing and hence the error occurs. You don't want to mix low level calls (e.g. Open with a stream) with the helper methods that eliminate the need for this (e.g. File.Read/Write/Append). Unless you need the low level access or you intend to do a lot of reads/writes then stick with the simple File.Read/Write/Append methods. Use the stream version (and only the stream version) along with StreamReader/Writer or BinaryReader/Writer when you need to interact with a file at a lower level.

    Given your code I would remove everything in that method except the call to File.AppendAllText. There is no reason to create the file because Append will do that automatically. To eliminate the 2 calls to Append put the line(s) you want to write in an array and then pass the single array to the File.AppendAllLines method. It is more efficient when writing multiple lines and reduces your method down to a single line of code (minus the try-catch you have).

    public void DeleteProductsListItems ()
    {
        var timestamp = DateTime.Now;
        var filename = $@"E:\MyLogFile\log_{timestamp.ToString("MM_dd_yyyy")}.txt";
    
                
        var lines = new[] {
                    $"Start deleting Products SP List: Date:{timestamp.ToLongDateString()}||{timestamp.ToLongTimeString()}",
                    $"Deletion has been : Date:{timestamp.ToLongDateString()}||{timestamp.ToLongTimeString()}",
            };
    
        try
        {
            File.AppendAllLines(filename, lines);
        } catch (Exception ex)
        {
            File.AppendAllLines(filename, new[] {
                $"Error Has been occurs while deleting Product List:{ex.Message}"
            });
        }
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 17, 2019 2:14 PM
    Moderator