locked
Error: File is in use by another process RRS feed

  • Question

  • User-73514677 posted

    Hi.

    I am creating a text file programmatically and trying to append the file with data. I have tried the below code, but I am getting error as File is in use by another process .

    static StreamWriter outfileJoblog;
     IDictionaryEnumerator idenumerator = myRes.GetEnumerator();
     string filesPath = string.Empty;
     string fullPath = string.Empty;
     
    while (idenumerator.MoveNext())
     {
       outfileJoblog = File.CreateText(targetPath + idenumerator.Key + ".del");
       fullPath = Path.GetFullPath(targetPath + idenumerator.Key + ".del");
    
                    var totalRecords = 0;
                    var tableIndex = 1;
                
                    totalRecords = dbhelper.ExecScalarCount(countqry);
                    int totalindex = totalRecords / 5000;
                    int offsetvalue = 0;
                    
                    DataSet ds = new DataSet();
                    StreamWriter sw = new StreamWriter(fullPath, true);
    
                    for (var index = 0; index <= totalindex; index++)
                    {
                        ds = dbhelper.ExecDataSetOffset(strQuery, offsetvalue);
    
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            object[] array = row.ItemArray;
                            for (int i = 0; i < array.Length - 1; i++)
                            {
                                sw.Write(array[i].ToString() + " ~ ");
                            }
                            sw.WriteLine(array[array.Length - 1].ToString());
                        }
    
                        tableIndex += 1;
                        offsetvalue += 5000;
                    }
    }

    How to fix this ?

    Thanks

    Tuesday, November 24, 2020 1:56 PM

Answers

  • User475983607 posted

    Your code opens the same file twice...

    Here...

    outfileJoblog = File.CreateText(targetPath + idenumerator.Key + ".del");

    And here

    StreamWriter sw = new StreamWriter(fullPath, true);

    Don't do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2020 4:02 PM
  • User1535942433 posted

    Hi venkatzeus,

    static StreamWriter outfileJoblog;
    outfileJoblog = File.CreateText(targetPath + idenumerator.Key + ".del");  

    File.CreateText() method create and open a file.It returns a StreamWriter object as you write. And StreamWriter method also create and open a file.

    So,it have the error of that file is in use by another process .

    https://www.c-sharpcorner.com/UploadFile/dbeniwal321/file-createtext-method-in-C-Sharp/

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 25, 2020 2:56 AM

All replies

  • User475983607 posted

    Your code opens the same file twice...

    Here...

    outfileJoblog = File.CreateText(targetPath + idenumerator.Key + ".del");

    And here

    StreamWriter sw = new StreamWriter(fullPath, true);

    Don't do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 24, 2020 4:02 PM
  • User1535942433 posted

    Hi venkatzeus,

    static StreamWriter outfileJoblog;
    outfileJoblog = File.CreateText(targetPath + idenumerator.Key + ".del");  

    File.CreateText() method create and open a file.It returns a StreamWriter object as you write. And StreamWriter method also create and open a file.

    So,it have the error of that file is in use by another process .

    https://www.c-sharpcorner.com/UploadFile/dbeniwal321/file-createtext-method-in-C-Sharp/

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 25, 2020 2:56 AM