TN888
TN888

Reputation: 7739

Error on writing file - it is used by other process

I tired to write in loop virus signatures to files. My code :

 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 is a WebClient object. Error callback :

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)

I don't know what is the reason of that error. No process is using my files, except my main thread, of course.

PS File base0001.rdtsignature has been created, but is empty.

Upvotes: 0

Views: 1689

Answers (4)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

File.Create returns the open FileStream, So when you create new StreamWriter it tries to access the file which is already opened in your process with File.Create results in IOException

Try this

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

Using statement ensures underlying stream of StreamWriter is closed when control exits Using. So no need to call sss.Close(); manually. using statement does it for you even when there is exception thrown.

Upvotes: 2

Frank Rundatz
Frank Rundatz

Reputation: 156

Just comment out:

File.Create(s2);

The problem is that File.Create(s2) returns a FileStream which leaves the file open. You are then trying to create a second stream to open the file for writing again which is why you get the error that the file is already open.

If you always want to create a new file, change your line that creates the StreamWriter to read:

StreamWriter sss = new StreamWriter(s2, false);

That will make it not append to an existing file but rather overwrite it.

Upvotes: 1

CSJ
CSJ

Reputation: 3971

Instead of:

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

Use:

StreamWriter sss = File.CreateText(s2);

Upvotes: 0

I4V
I4V

Reputation: 35363

You don't close the file created by File.Create(s2);.

Try using( File.Create(s2) ); or File.Create(s2).Close();

Upvotes: 1

Related Questions