none
“is in use by other process”? RRS feed

  • Question

  • I have a log file for my application and the log file has maximum of 10 log files that can be created. The code snippet is as follows,

    Stream logFileStream = null;
            string file = Config.LOG_FILE;
            m_strLogfile = Config.LOG_FILE + Config.LOG_FILE_EXT; 
    
     while (File.Exists(m_strGeneralFilePath + file + Config.LOG_FILE_EXT))
            {
                logFileStream = File.OpenRead(m_strGeneralFilePath + file + Config.LOG_FILE_EXT);
               // if(!logFileStream.Length.Equals(logSize))
                if (  logFileStream.Length > Convert.ToInt32(logSize)) // approx. 1MB 1.000.000
                {
                    for (int i = 9; i > 0; i--)
                    {
                        if (i == 9)
                        {
                            if (File.Exists(Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                            {
                                File.Delete(Config.LOG_FILE + i + Config.LOG_FILE_EXT);
                            }
                        }
                        if (File.Exists(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                        {
                            System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + (i + 1) + Config.LOG_FILE_EXT);
                        }
                    }
                    System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + "1" + Config.LOG_FILE_EXT);
    
                File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT);
    
    
                }               
            }
            if (logFileStream != null)
            {
                logFileStream.Close();
                logFileStream.Dispose();
            }

    As soon as it comes to System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + "1" + Config.LOG_FILE_EXT); it hits an exception saying "it is used by some other process", how can I get rid of this? Please help me in this. Thanks in advance.


    Roopini

    Monday, September 2, 2013 9:55 AM

Answers

  • the small change i made was that the File.Create () returns stream, and was not closing the file anywhere.
    So the change is 

       using (FileStream fs = File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT))
                        {
                            fs.Flush();
                            fs.Close();
                        }


    Roopini

    • Marked as answer by Roopini Monday, September 2, 2013 10:53 AM
    Monday, September 2, 2013 10:53 AM

All replies

  • Roopini,

    this will be based on the fact that you have opened the same file for checking the length which you then are trying to move. You need to close the stream before you can move the file like:

            string file = Config.LOG_FILE;
            m_strLogfile = Config.LOG_FILE + Config.LOG_FILE_EXT;
    
     	while (File.Exists(m_strGeneralFilePath + file + Config.LOG_FILE_EXT))
            {
            	long logFileStreamLength=0;
            	using (Stream logFileStream = File.OpenRead(m_strGeneralFilePath + file + Config.LOG_FILE_EXT))
            	{
            		logFileStreamLength = logFileStream.Length;
            	}
               // if(!logFileStream.Length.Equals(logSize))
                if (  logFileStreamLength > Convert.ToInt32(logSize)) // approx. 1MB 1.000.000
                {
                    for (int i = 9; i > 0; i--)
                    {
                        if (i == 9)
                        {
                            if (File.Exists(Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                            {
                                File.Delete(Config.LOG_FILE + i + Config.LOG_FILE_EXT);
                            }
                        }
                        if (File.Exists(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                        {
                            System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + (i + 1) + Config.LOG_FILE_EXT);
                        }
                    }
                    System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + "1" + Config.LOG_FILE_EXT);
    
                File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT);
    
    
                }               
            }



    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    My Forum Threads


    • Edited by Heslacher Monday, September 2, 2013 10:10 AM
    Monday, September 2, 2013 10:02 AM
  •   private static Stream logFileStream = null; i have declared it as a class level variable,

    I also tried this 

     if (File.Exists(m_strGeneralFilePath + file + Config.LOG_FILE_EXT))
                {
                    if (logFileStream == null)
                    {
                        logFileStream = new FileStream(m_strGeneralFilePath + file + Config.LOG_FILE_EXT, FileMode.Open, FileAccess.ReadWrite);
                    }                
                    // if(!logFileStream.Length.Equals(logSize))
                    if (logFileStream.Length > Convert.ToInt32(logSize)) // approx. 1MB 1.000.000
                    {
                        logFileStream.Close();
                        logFileStream.Dispose();
                        for (int i = 9; i > 0; i--)
                        {
                            if (i == 9)
                            {
                                if (File.Exists(Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                                {
                                    File.Delete(Config.LOG_FILE + i + Config.LOG_FILE_EXT);
                                }
                            }
                            if (File.Exists(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                            {
                               
    
                                System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + (i + 1) + Config.LOG_FILE_EXT);
                            }
                        }
                        System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + "1" + Config.LOG_FILE_EXT);
    
                        File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT);
                    }
                }

    still it says same that it is used by another process


    Roopini


    • Edited by Roopini Monday, September 2, 2013 10:25 AM changes
    Monday, September 2, 2013 10:22 AM
  • Hi Heslacher,

    I tried the solution you replied with, but 

    that din't work as well :-(


    Roopini

    Monday, September 2, 2013 10:33 AM
  • the small change i made was that the File.Create () returns stream, and was not closing the file anywhere.
    So the change is 

       using (FileStream fs = File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT))
                        {
                            fs.Flush();
                            fs.Close();
                        }


    Roopini

    • Marked as answer by Roopini Monday, September 2, 2013 10:53 AM
    Monday, September 2, 2013 10:53 AM