locked
Problem to path RRS feed

  • Question

  • Hi,
    What can be reason to this

    System.IO.IOException was unhandled
      HResult=-2147024864
      Message=The process cannot access the file 'C:\App 13\bin\Debug\err05.txt' because it is being used by another process.
      Source=mscorlib
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
           at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
           at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
           at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
           at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
           at WinFormApp.Form1.CreateDoc_Click(Object sender, EventArgs e) in c:\App 13\Form1.cs:line 90
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.PerformClick()
           at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
           at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
           at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
           at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
           at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
           at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at WinFormsApp.Program.Main() in c:\App 13\Program.cs:line 20
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 



    with this line?
                    using (StreamWriter sw = new StreamWriter(@"err05.txt", true, Encoding.Unicode))



    Many Thanks & Best Regards, Hua Min

    Saturday, May 27, 2017 2:26 PM

Answers

  • Hi HuaMin Chen,

    I make a simple example to test your code.

    If I create or open err05.txt file before run the project, there is no error.

    But if there are more thread invoke the StreamWriter, I will get the error message below.

    An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
    
    Additional information: The process cannot access the file 'C:\Users\v-wezan\Documents\Visual Studio 2015\Projects\ConsoleApplication\threadsTest\bin\Debug\err05.txt' because it is being used by another process.

    Please make the method which include the code below only be invoke in same time.

     using (StreamWriter sw = new StreamWriter(@"err05.txt", true, Encoding.Unicode))

    For thread synchronization, please refer to the MSDN article.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/threading/thread-synchronization

    Best Regards,

    Wendy


    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.

    • Marked as answer by Jackson_1990 Friday, June 9, 2017 7:42 AM
    Tuesday, May 30, 2017 5:54 AM

All replies

  • Hi Hua,

    Have you opened the file elsewhere in the code and didn't close it?

    The process cannot access the file 'C:\App 13\bin\Debug\err05.txt' because it is being used by another process.

    The error says that the file is already opened or used by another process

     

    Greetings Rinaldo


    Greetings from Amsterdam the Netherlands

    Saturday, May 27, 2017 3:19 PM
  • Thanks Ranaldo.

    How to trace to see where the text file is being opened exclusively?


    Many Thanks & Best Regards, Hua Min

    Sunday, May 28, 2017 2:28 PM
  • Have you already searched all of your sources for “err05” using Find in Files feature of Visual Studio?

    For example, maybe you try to open another exclusive stream inside the above using block.

    Sunday, May 28, 2017 6:40 PM
  • Hi,
    I cannot find other codes referring to "err05", within "entire solution".

    Many Thanks & Best Regards, Hua Min

    Monday, May 29, 2017 3:20 AM
  • Maybe the above using is performed repeatedly because of some recursive execution. Make sure that this does not happen. For investigations, put a breakpoint and use the Debugger. If the file is not open by other processes (e.g. Notepad) or by your program, then it should not give any error on first execution.

    Monday, May 29, 2017 5:12 AM
  • Hi HuaMin Chen,

    I make a simple example to test your code.

    If I create or open err05.txt file before run the project, there is no error.

    But if there are more thread invoke the StreamWriter, I will get the error message below.

    An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
    
    Additional information: The process cannot access the file 'C:\Users\v-wezan\Documents\Visual Studio 2015\Projects\ConsoleApplication\threadsTest\bin\Debug\err05.txt' because it is being used by another process.

    Please make the method which include the code below only be invoke in same time.

     using (StreamWriter sw = new StreamWriter(@"err05.txt", true, Encoding.Unicode))

    For thread synchronization, please refer to the MSDN article.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/threading/thread-synchronization

    Best Regards,

    Wendy


    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.

    • Marked as answer by Jackson_1990 Friday, June 9, 2017 7:42 AM
    Tuesday, May 30, 2017 5:54 AM
  • Sorry to that, I do not find out any other codes that are opening "err05.txt".

    Many Thanks & Best Regards, Hua Min

    Tuesday, May 30, 2017 7:03 AM
  • Hi HuaMinChen,

    Could your provide sample code about how to invoke this StreamWriter method? For us to test to repeat the error like yours.

    Best Regards,

    Wendy


    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.

    Tuesday, May 30, 2017 1:15 PM
  • Hi Wendy,

    Can I have your own Email address to send you the project?


    Many Thanks & Best Regards, Hua Min

    Tuesday, May 30, 2017 2:19 PM
  • Hi HuaMin Chen,

    Please share a simple project in OneDrive which could repeat the error for us to test.

    Here is a link about how to upload the file in OneDrive.

    https://support.office.com/en-us/article/Share-OneDrive-files-and-folders-9fcc2f7d-de0c-4cec-93b0-a82024800c07

    Best Regards,

    Wendy


    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, May 31, 2017 10:18 AM
  • Hi,
    Can there be one Email address, to receive it, and this is the way I expect to have?

    Many Thanks & Best Regards, Hua Min

    Thursday, June 1, 2017 1:32 AM
  • Hi HuaMinChen,

    According to our policy, it would be better to upload your project on OneDrive.

    Or your could provide some sample code directly which could repeat the error.

    Best Regards,

    Wendy


    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.

    Thursday, June 1, 2017 1:55 AM
  • Hi,
    Can you please run it, and choose "CreateDoc" button, then you'll get the error?


    Many Thanks & Best Regards, Hua Min


    Thursday, June 1, 2017 1:57 AM
  • Hi HuaMin Chen,

    It is not suitable for our policy. If you upload the file on OneDrive, all the communities could helpful you to solve the problem.

    If your concerns about the security, you could make a simple code which could reproduce the error. Or you could provide the code line which you get the error for use to test.

    Best Regards,

    Wendy


    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.

    Friday, June 9, 2017 7:09 AM