Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-08-10 01:55:00

ParadoxInTheVoid
Member
From: ̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺
Joined: 2015-02-18
Posts: 341

[Help] IOException

I'm trying to create a new text file and write text to it but I get an error saying something like

This file cannot be accessed because it is being used by another process.

How do I make it to where it is not longer being used by the other process.

Also: Here is the code i'm currently using:

SaveFileDialog saveNote = new SaveFileDialog();
            saveNote.Filter = "Supported Files|*.note;*.n;*.txt|All Files|*.*";
            saveNote.FilterIndex = 1;
            saveNote.Title = "Save your note.";
            saveNote.ShowDialog();
            if (saveNote.FileName != null)
            {
                if (!File.Exists(saveNote.FileName))
                {
                    File.Create(saveNote.FileName);
                    File.WriteAllText(saveNote.FileName, notesyPad.Text);
                }
                else if (File.Exists(saveNote.FileName))
                {
                    File.WriteAllText(saveNote.FileName, notesyPad.Text);
                }
            }

Thanks.


signature.png

Offline

#2 2015-08-10 02:08:08, last edited by Xfrogman43 (2015-08-10 02:08:24)

Xfrogman43
Member
From: need to find a new home
Joined: 2015-02-15
Posts: 4,174

Re: [Help] IOException

You can't have the file opened... Close it then try again.


zsbu6Xm.png thanks zoey aaaaaaaaaaaand thanks latif for the avatar

Offline

#3 2015-08-10 02:13:49, last edited by ParadoxInTheVoid (2015-08-10 02:14:19)

ParadoxInTheVoid
Member
From: ̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺
Joined: 2015-02-18
Posts: 341

Re: [Help] IOException

Xfrogman43 wrote:

You can't have the file opened... Close it then try again.

I don't have the file opened. After creating the file i'm trying to write to it, how do I make my program stop accessing the file so it can then write to it?


signature.png

Offline

#4 2015-08-10 02:15:27, last edited by eeisold (2015-08-10 02:20:01)

eeisold
Member
Joined: 2015-06-14
Posts: 202

Re: [Help] IOException

File.WriteAllText doesn't need a created file. It can take care of itself.

Also, xfrogman43 is correct, apparent or not.

File.Create() is different from the other functions in that it does something rather peculiar. It's not actually creating a file. It's creating a STREAM. The stream is causing the IO exception because it has the rights to the file. Sadly, it longs for the day to be disposed of properly. This code doesn't do that.
So if you wanted, you'd call

File.Create(saveNote.FileName).Dispose();

which would be redundant.

Offline

#5 2015-08-10 02:18:51

ParadoxInTheVoid
Member
From: ̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺
Joined: 2015-02-18
Posts: 341

Re: [Help] IOException

eeisold wrote:

File.WriteAllText doesn't need a created file. It can take care of itself.

Also, xfrogman43 is correct, apparent or not.

Thanks, never knew that!
I know he is correct but I was saying that it wasn't open in some other program (that isn't VS/My Program).


signature.png

Offline

#6 2015-08-10 03:06:51

Xfrogman43
Member
From: need to find a new home
Joined: 2015-02-15
Posts: 4,174

Re: [Help] IOException

ParadoxInTheVoid wrote:
eeisold wrote:

File.WriteAllText doesn't need a created file. It can take care of itself.

Also, xfrogman43 is correct, apparent or not.

Thanks, never knew that!
I know he is correct but I was saying that it wasn't open in some other program (that isn't VS/My Program).

Never read the code, its just that I have had that error before, closed notepad/whatever and it works fine.


zsbu6Xm.png thanks zoey aaaaaaaaaaaand thanks latif for the avatar

Offline

#7 2015-08-10 04:52:49

Different55
Forum Admin
Joined: 2015-02-07
Posts: 16,575

Re: [Help] IOException

Locked on request of OP.


"Sometimes failing a leap of faith is better than inching forward"
- ShinsukeIto

Offline

Different551439178769528752

Board footer

Powered by FluxBB

[ Started around 1732778751.1246 - Generated in 0.138 seconds, 10 queries executed - Memory usage: 1.45 MiB (Peak: 1.58 MiB) ]