0

wise community! I ask you for help again in my, maybe simple, situation.

So. I have some file (.ini-file) with some text lines. Example of file:

[SomeGuy]
Name = Alex;
Surname = Newbie;
EMails = "-mails=@mail1@somemail.com;@mail2@somemail.com;" -MainEmail=mail1@somemail.com
Visits = 0;
...

I'm trying to find a solution to: 1) Replace:

EMails = "-mails=@mail1@somemail.com;@mail2@somemail.com;" -MainEmail=mail1@somemail.com

to

EMails = "-mails=@mail1@somemail.com;@mail2@somemail.com;" -MainEmail=NewEmail

2) Or delete:

EMails = "-mails=@mail1@somemail.com;@mail2@somemail.com;" -MainEmail=mail1@somemail.com

and fit in its place:

EMails = "-mails=@mail1@somemail.com;@mail2@somemail.com;" -MainEmail=NewEmail

Where 'NewEmail' - is a new variable value taken from user's input.

I searched here for some solutions, found some and applied applied in this example:

string IniFile = @"S:\My.ini";
    var fileStrings = File.ReadAllLines(IniFile).ToList();

    for (var i = fileStrings.Count - 1; i >= 0; i--)
    {
        if (fileStrings[i].StartsWith("EMails"))
        fileStrings.RemoveAt(i);
    }

string NewEmail = "EMails = \"-mails=@mail1@somemail.com;@mail2@somemail.com;\" -MainEmail"+EMailField.Text;
File.AppendText(IniFile).Write(NewEmail);
File.WriteAllLines(IniFile, fileStrings);

EMailField is users input field, from which I take written value (EMailField.Text).

In this example I get error after input: System.IO.IOException: The process cannot access the file 'S:\My.ini' because it is being used by another process.

But it is not open, used or smth else. I also try to write all line from original file to another by applying this:

string NewIniFile = @"S:\MyNew.ini";
...
File.WriteAllLines(NewIniFile, fileStrings);

But again get same error - "...'S:\My.ini' is being used by another process".

Using Visual Studio 2019 and creating Windows Forms application.

Please poke me in the eye what am I doing wrong.

Auditive
  • 1,607
  • 1
  • 7
  • 13
  • The code looks good to me, the only problem seems to be file access. Do you have permissions to read the file - can you open it in explorer without windows asking for admin priviledges? – anonymousdotnetdeveloper666 Aug 09 '19 at 08:46
  • File used it s not a sign that the file is open but may be used by another windows service or task and also may be you don't have access to modify the file – Amine Aug 09 '19 at 08:53
  • 1
    `File.AppendText()` opens a file AND KEEPS IT OPEN. You need to store the return value in a variable and close it after you've written to it (preferably via `using`). Otherwise, it will remain open until the garbage collector runs, which - as you have discovered - is *after* you tried to use it in `File.WriteAllLines()`. In the duplicate question, pay attention to the "Please note a common error we see very often on StackOverflow" part! – Matthew Watson Aug 09 '19 at 08:54
  • Matthew, thanks for involve!. I solve it just by: fileStrings.RemoveAt(3); fileStrings.Insert(3,NewString); because EMails is always on 3rd line :D – Auditive Aug 09 '19 at 09:08
  • @Auditive I would not suggest relying on line order, Ini files usually do not require that. Finding the correct line is easy enough – anonymousdotnetdeveloper666 Aug 09 '19 at 09:31

0 Answers0