0

I am trying to create a file and write in it using C# code below:

string machinename;

// CHECKING IF THE DIRECTORY EXISTS OR NOT -- IF NOT CREATE ONE
if (!Directory.Exists(Application.StartupPath + @"\Settings\")) 
 Directory.CreateDirectory(Application.StartupPath + @"\Settings\");

// CHECKING IF THE FILE EXISTS OR NOT -- IF NOT CREATE ONE
if (!File.Exists(Application.StartupPath + @"\Settings\info.txt"))
 File.CreateText(Application.StartupPath + @"\Settings\info.txt");

// READING THE CONTNET OF THE FILE AND WRITING IT IN A STRING 
machinename = File.ReadAllText(Application.StartupPath + @"\Settings\info.txt");

// IF THE CONTENT IS EMPTY THEN WRITE IN THE FILE FOR FURTHUR USE
if (machinename == "")
{
   File.WriteAllText(Application.StartupPath + @"\Settings\info.txt", Environment.MachineName);
   machinename = Environment.MachineName;
}

// Do Stuff with "machinename"

But I get the following error :

Message = The process cannot access the file because it is being used by another process.

And it throws the exception on this line :

machinename = File.ReadAllText(Application.StartupPath + @"\Settings\info.txt");

Exactly after creating the file.

(This means if the exists but it is empty it can read it and write in it, But it cant create and read from it).

That is because functions ReadAllText and WriteAllText use the embedded File.Close() and by what I see File.CreateText() does not have it.

Now the question is:

How can I makes sure that the process Closes the file after creating it?

NOTES:

  1. No I am not using the file in other places of the code.
  2. I have read other answers about this particular kind of error in here but it does not answer my question
  3. The link above mostly uses StreamWriter which I don not want to use because my string is not a large one to process and it is easier to use this way.
4
  • 1
    You need to close the StreamWriter. That's done for you if you use a using block, or you can call Close() manually. Commented May 29, 2021 at 6:19
  • @TiesonT. I am not using the streamWriter. and on the other hand File does not have a Close() property Commented May 29, 2021 at 6:21
  • 3
    Yeah, you are: learn.microsoft.com/en-us/dotnet/api/… - StreamWriter is what's returned by File.CreateText(). Commented May 29, 2021 at 6:23
  • Check this link stackoverflow.com/questions/5156254/… Commented May 29, 2021 at 6:27

2 Answers 2

2

From documentation public static System.IO.StreamWriter CreateText (string path); (a StreamWriter is returned) and so on (reading documentation) ... a StreamWriter is a (inherits from) TextWriter which is IDisposable, so you should be doing this...

if (!File.Exists(Application.StartupPath + @"\Settings\info.txt"))
{
    var sw = File.CreateText(Application.StartupPath + @"\Settings\info.txt");
    sw.Dispose();
}

or better yet

if (!File.Exists(Application.StartupPath + @"\Settings\info.txt"))
    using (var sw = File.CreateText(Application.StartupPath + @"\Settings\info.txt"))
    {/* do nothing, but end of using does the Dispose() for you */}

or best of all

if (!File.Exists(Application.StartupPath + @"\Settings\info.txt"))
    File.WriteAllText(Application.StartupPath + @"\Settings\info.txt", 

because WriteAlltext() doesn't need the file to exist before it is called.

(Or you could use that StreamWriter to write the machine name...)

Sign up to request clarification or add additional context in comments.

Comments

2

The problem is File.CreateText you don't have to create the text before calling File.WriteAllText, it will create the file if it doesn't exist. just try to delete File.Createtext

Use the code below:

if (!File.Exists(mypath))
    File.WriteAllText(mypath, myString);
else
    mynewString = File.ReadAllText(mypath);

3 Comments

One reason that I am checking the existence of the file is because I don't want the application to always write in file... Simply I want to say if it is there... skip the writing part and read from it. But it works fine
i understand what you say , but you could just check for existence and don't create File , is it clear to You what is my purpose ? i want you to remove CreateText from your Code , and your Code Will Work Fine Without Any exception.
Yeap I just did that and I checked it and I write in it no creation needed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.