c# - File.WriteAllText the process cannot access the file because it is being used by another process -


I am using this code to write in a text file:

  Int num; Streamer writer writer 2; Bull flag = true; String str = ""; If (flag == file.exe (this.location)) {file. Write all text (this.location, string.Empty); New fileinfo (this.location). Open (FileMod. Truncate). Stop it (); (Author2 = this.SW = using the file .Updated text (this.location)) {this.SW.WriteLine ("calculation =" + this.Qount); For (num = 0; num & lt; this.Qount; num ++) {this.SW.WriteLine (this.image [num]); } This.SW. Close (); }}  

But I'm not calling the system.OOException that the process can not access the file because it is being used on this code by another process :

  file. Write all text (this.location, string.Empty);  

However, I check the text file and I think it was updated.

is counted if items .

  if you (file.exext (this.location)) file. Type new lines (this. Location, this. Item);  

If it is not and you are tapping items from each object with toasting () you can do this:

  if (file example (this.location)) {var textLines = items. Selection (x = & gt; x.ToString ()); File. Write All Rows (this location, text line); }  

This should fix your problem with the file being locked because it is accessing the file only once, where your original code is opening 3 times.

Edit : There is a cleaner version using the stream here, keeping in mind your addition of just adding a "count" line.

  if (file.exeventions (this.location)) {(var fileInfo = new FileInfo (this.location)) {{using var writer = fileInfo} (item) Item (item);}}}  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -