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.
rioV8
  • 24,506
  • 3
  • 32
  • 49
Atrin Noori
  • 311
  • 3
  • 12
  • 1
    You need to close the StreamWriter. That's done for you if you use a `using` block, or you can call `Close()` manually. – Tieson T. May 29 '21 at 06:19
  • @TiesonT. I am not using the streamWriter. and on the other hand ```File``` does not have a ```Close()``` property – Atrin Noori May 29 '21 at 06:21
  • 3
    Yeah, you are: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createtext?view=net-5.0 - StreamWriter is what's returned by `File.CreateText()`. – Tieson T. May 29 '21 at 06:23
  • Check this link https://stackoverflow.com/questions/5156254/closing-a-file-after-file-create – Biju Kalanjoor May 29 '21 at 06:27

2 Answers2

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...)

AlanK
  • 1,827
  • 13
  • 16
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);
Atrin Noori
  • 311
  • 3
  • 12
  • 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 – Atrin Noori May 29 '21 at 06:38
  • 1
    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. – sina ajilyan May 29 '21 at 06:43
  • Yeap I just did that and I checked it and I write in it no creation needed – Atrin Noori May 29 '21 at 06:44