C# System.IO.IOException:进程无法访问文件';C:\RDS\RDS.txt';因为它正被另一个进程使用

C# System.IO.IOException:进程无法访问文件';C:\RDS\RDS.txt';因为它正被另一个进程使用,c#,forms,C#,Forms,运行应用程序后,几分钟后我收到此错误消息。为什么?如果你能帮我解答,我将不胜感激 以下是我的Form1的源代码: (错误消息) 有关调用的详细信息,请参阅此消息的结尾 即时(JIT)调试,而不是此对话框。 **************例外文本************** System.IO.IOException:进程无法访问文件“C:\RDS\RDS.txt”,因为另一个进程正在使用该文件。 在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符

运行应用程序后,几分钟后我收到此错误消息。为什么?如果你能帮我解答,我将不胜感激

以下是我的Form1的源代码:

(错误消息)

有关调用的详细信息,请参阅此消息的结尾
即时(JIT)调试,而不是此对话框。
**************例外文本**************
System.IO.IOException:进程无法访问文件“C:\RDS\RDS.txt”,因为另一个进程正在使用该文件。
在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项选项、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.StreamWriter.CreateFile(字符串路径、布尔追加、布尔检查主机)
位于System.IO.StreamWriter..ctor(字符串路径、布尔追加、编码编码、Int32 bufferSize、布尔校验主机)
在System.IO.StreamWriter..ctor处(字符串路径)
在WindowsFormsApplication2.Form1.timer1_勾选(对象发送方,事件参数e)
在System.Windows.Forms.Timer.OnTick(EventArgs e)中
在System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)中
**************加载的程序集**************
mscorlib
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
txtRDS
程序集版本:1.0.0.0
Win32版本:1.0.0.0
代码库:file:///C:/Users/hevi%20sms/Downloads/txtRDS.exe
----------------------------------------
System.Windows.Forms
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
系统
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
系统图
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
可达性
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
系统配置
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
命名空间
程序集版本:4.0.0.0
Win32版本:4.5.27.0生成者:FX453PREVIEWREL
代码库:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
**************JIT调试**************
要启用即时(JIT)调试,请为
应用程序或计算机(machine.config)必须具有
在system.windows.forms节中设置的值。
应用程序还必须通过调试进行编译
启用。
例如:
启用JIT调试时,任何未处理的异常
将发送到计算机上注册的JIT调试器
而不是由此对话框处理。

您是否在其他应用程序中打开了文本文件?听起来好像有一个程序打开了文本文件,阻止你的程序访问它。这可以让您了解哪个应用程序正在这样做:

你检查过我的答案了吗?这对你有帮助吗?是的,我读了你的答案,我忙于工作,现在我没有时间回复你,我正在回复你的答案1秒钟,我现在无法阅读@SachithMW的答案。有什么意见吗?没有,我没有在另一个应用程序中打开那个文本文件。因为我知道,如果我在记事本中打开文本文件,检查form1的文本框文本是否已成功写入并保存,计时器重置几秒钟后就会抛出错误。我以前做过测试。应用程序正在保存到文本文件,textbox2显示文本文件的内容,因此我不需要在记事本中打开。但应用程序应在计算机中7/24运行,且不应关闭。因为默认情况下,文本文件每10秒保存一次textbox1.text或textbox4.text的数据。但在1-2小时后抛出错误。如果您打算每10秒写入同一个文件,并且该文件仅由单个应用程序在整个运行时间内使用。最好是保持文件处于打开状态,直到应用程序关闭后再关闭。这也应该解决你的问题。我理解,是的,它应该是相同的文本文件只。因为有一个设备可以读取该文本文件的内容。所以我需要使用“close”。我已经检查了你的链接,没有进程阻止访问。我还在“资源监视器”和“进程浏览器”屏幕上等待
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.IO.IOException: The process cannot access the file 'C:\RDS\RDS.txt' because it is being used by another process.
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)
at WindowsFormsApplication2.Form1.timer1_Tick(Object sender, EventArgs e)
at System.Windows.Forms.Timer.OnTick(EventArgs e)
at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
txtRDS
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/hevi%20sms/Downloads/txtRDS.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
 CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.5.27.0 built by: FX453PREVIEWREL
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.