0

我厌倦了将循环病毒签名写入文件。我的代码:

 for (int i = 0; i < liczba; i++)
                {
                    int current = i + 1;
                    string xxx = w.DownloadString("xxx(hidden)");
                    if (xxx != "0")
                    {
                        string[] wirus = xxx.Split("|||".ToCharArray());
                        string s2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_RDTSignatures", "base000" + current.ToString() + ".rdtsignature");
                        File.Create(s2);
                        StreamWriter sss = new StreamWriter(s2); //that's crash line
                        sss.WriteLine("hidden");
                        sss.WriteLine(wirus[0]);
                        sss.WriteLine(wirus[1]);
                        sss.Close();
                        File.Encrypt(s2);
                    }
                }

w是一个WebClient对象。错误回调:

System.IO.IOException: Process cannot access file : „C:\Users\Pluse Konto\Documents\Visual Studio 2010\Projects\Radzik Diagnostic Tool\Radzik Diagnostic Tool\bin\Debug\_RDTSignatures\base0001.rdtsignature”, because it is used by other process.
   w System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   w 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)
   w System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   w System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   w System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   w System.IO.StreamWriter..ctor(String path)
   w Radzik_Diagnostic_Tool.Updates.timer1_Tick(Object sender, EventArgs e) w C:\Users\Pluse Konto\documents\visual studio 2010\Projects\Radzik Diagnostic Tool\Radzik Diagnostic Tool\Updates.cs:line 69
   w System.Windows.Forms.Timer.OnTick(EventArgs e)
   w System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   w System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我不知道该错误的原因是什么。当然,除了我的主线程之外,没有任何进程在使用我的文件。

PS 文件base0001.rdtsignature已创建,但为空。

4

4 回答 4

2

File.Create返回 open FileStream,因此当您创建新StreamWriter文件时,它会尝试访问已在您的进程中打开的文件,File.Create结果为IOException

尝试这个

using (StreamWriter sss = new StreamWriter(File.Create(s2)))
{
    //Make use of sss
}

StreamWriterusing 语句可确保在控制退出时关闭底层流Using。所以不需要 sss.Close();手动调用。即使抛出异常, using 语句也会为您执行此操作。

于 2013-08-24T19:00:31.117 回答
1

您不会关闭由File.Create(s2);.

尝试using( File.Create(s2) );File.Create(s2).Close();

于 2013-08-24T18:48:26.560 回答
1

只需注释掉:

文件.创建(s2);

问题是 File.Create(s2) 返回一个 FileStream ,它使文件保持打开状态。然后,您尝试创建第二个流以打开文件以再次写入,这就是您收到文件已打开的错误的原因。

如果您总是想创建一个新文件,请将创建 StreamWriter 的行更改为:

StreamWriter sss = new StreamWriter(s2, false);

这将使它不会附加到现有文件,而是覆盖它。

于 2013-08-24T18:59:53.927 回答
0

代替:

File.Create(s2);
StreamWriter sss = new StreamWriter(s2); //that's crash line

利用:

StreamWriter sss = File.CreateText(s2);
于 2013-08-24T18:54:56.993 回答