I have this problem in C# that this code gets executed, and it does not go through, it gives me an error "File being used by another process" (or similar). I've tried looking for answers on Stack, but I couldn't quite put together all of it. I attempted to insert some more code (like they said) there, but just hands me more errors. Please help, many thanks in advance!
Below is my current code =>
// let's create example.py
string path = @"C:\example.py";
var stream = new FileStream(path, FileAccess.Read);
var reader = new StreamReader(stream);
if (!File.Exists(path)) {
File.Create(path);
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("# code to insert into target file");
} else if (File.Exists(path)) {
System.IO.File.WriteAllText(@"C:\example.py", string.Empty); // clear contents
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("# more code to insert into example.py");
}
This is my original code (no fix attempts) =>
// let's create example.py
string path = @"C:\example.py";
if (!File.Exists(path)) {
File.Create(path);
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("# code to insert into target file");
} else if (File.Exists(path)) {
System.IO.File.WriteAllText(@"C:\example.py", string.Empty); // clear contents
TextWriter tw = new StreamWriter(path, true);
tw.WriteLine("# more code to insert into example.py");
}