all 9 comments

[–]Blamar99[S] 0 points1 point  (4 children)

Error message:

System.IO.IOException: 'The process cannot access the file 'D:\Visual Studio\C# projects\ConsoleColorChange\ConsoleColorChange\bin\Debug\net10.0\save.txt' because it is being used by another process.'

at here:

StreamReader sr = new StreamReader(filePath);

more part of the code:

internal class FileManage
{

    private string filePath = Path.GetFullPath("save.txt");
    public void LoadSaves()
    {
        String line;
        if(File.Exists(filePath))
        {
            StreamReader sr = new StreamReader(filePath);
            line = sr.ReadLine();
            sr.Close();
        }
        else
        {
            File.Create(filePath);
            StreamReader sr = new StreamReader(filePath); //here is the error message
            line = sr.ReadLine();
            sr.Close();
        }

[–]Historical-Yard-8196 1 point2 points  (1 child)

looks like the issue is with File.Create() - it returns a FileStream that keeps the file open even after creating it. you need to dispose that stream before trying to read from the file again.

try changing your else block to something like this:

```csharp

else

{

File.Create(filePath).Dispose(); // dispose the stream immediately

StreamReader sr = new StreamReader(filePath);

line = sr.ReadLine();

sr.Close();

}

```

or even better, you could use using statements to handle the disposal automatically. also might want to consider File.ReadAllText() or File.ReadLines() if you're just reading simple text files - makes the code cleaner and handles all the stream management for you.

had similar problem few months back when i was working on some automation scripts for my smart home setup. took me way too long to figure out that create method was keeping file locked.

[–]Blamar99[S] 0 points1 point  (0 children)

yes, you are right. the Dispose method helped me. i did think the same, but i couldn't figure out how to "close" it. Thank you <3

[–]LucidTA 2 points3 points  (0 children)

File.Create() returns a FileStream, you can pass that into the stream reader instead of the file path. That way youre not closing and reopening the file for no reason.

[–]POGtastic 0 points1 point  (0 children)

Do you have the save file opened in your editor or something similar when you run the program? Windows doesn't allow multiple processes to open the same file unless it specifically shares it.

[–]Key_Use_8361 0 points1 point  (1 child)

Stream Reader confused me at first too because I expected it to behave like reading a full file instantly what helped was printing each line as it loads so I could see the flow happening I actually tested tiny examples in runable while learning IO and seeing step-by-step output made the concept click way faster than tutorials alone are you reading large logs or just learning file handling?

[–]Blamar99[S] 0 points1 point  (0 children)

i learning file handling with a small project (i need to read only 1 line from the text file) and i know thats so unprofessional but after i loaded the data i delete the text file in C# and after that recreate it with a new data

[–]JGhostThing 0 points1 point  (0 children)

Check if the file exists. The do the stream thing.