Click here to Skip to main content
15,918,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I need to edit part of a specific line in a text file.

The line in the text file I want to edit is:
login = '. login username password\r\n'

I want to replace the username and password with the ones entered in my form text boxes.

The current username and password fields in the above line in the text file could be anything.

How can I replace the above line with my form textbox username and password strings without knowing exactly what the current username and password are??

I have used the code below but this only replaces strings that I specify and that I know :(

public static bool Replace(ref string file, ref string searchFor, ref string replaceWith)
        {
            try
            {   
                StreamReader reader = new StreamReader(file);
                string contents = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
                contents = Regex.Replace(contents, searchFor, replaceWith);
                StreamWriter writer = new StreamWriter(file);
                writer.Write(contents);
                writer.Close();
                writer.Dispose();
                return true;
            }
any help would be appreciated. Many thanks!
Posted
Updated 20-Nov-10 1:42am
v2
Comments
Abhinav S 20-Nov-10 7:42am    
Pre tags added.

1 solution

Hi Sach!

if the line you're interested in starts with "login = "
I'd just search for that and when it's found write out the
newly assembled line:
String accountInfo = "login=" + login + " " + password. That should do the trick.



Now I've got it:

searchFor = "(\\s+login\\s*=\\s*)(.+)(\\r\\n)";
replaceWith = "$1" + userName + " " + password + "$3");
contents = "# This is a test\r\n" +
           "login = Will be replaced\r\n" +
           "# more test lines\r\n" +
           "login2 = Wont be touched\r\n"
           "# more test\r\n";


The $1 and $3 part of the replace expression correspond to the first and the third group.

Cheers

Manfred
 
Share this answer
 
v5
Comments
sach262 20-Nov-10 8:05am    
Hi Manfred

Thanks for the reply

I have tried that , but still have the same problem. Instead of replacing the entire login line , it only replaces the "login= " part of that line and not the entire line.
sach262 20-Nov-10 12:16pm    
wow, ok im going to try this right now and I will let you know of the outcome! thank you!
sach262 20-Nov-10 12:59pm    
I tried your new suggestion. But when I check the text file afterwards the entire login = '. login username password\r\n' line has disappeared. what do you think?

Thanks for all the help
Manfred Rudolf Bihy 20-Nov-10 13:43pm    
I think you need to debug this and let me know verbatim what the values for contents, searchFor and replaceWith are that are used in the last modification your code.
sach262 20-Nov-10 13:53pm    
Sorry my apologies! It does work , the line does not disappear but after the operation that line in the text file moved around. It doesn't keep its place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900