Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a text file. My requirement is to replace the particular line completely with the input. My problem is only half of that line is constant and the remaining half can be anything.



So far I have tried the below

C#
static void Main(string[] args)
        {
            //1.Copy the file from one location to another location
            string sourcePath = @"D:\\SQLConfigurationFile\\ConfigurationFile.ini";
            string destinationPath = @"C:\\Temporary\\ConfigurationFile.ini";
            File.Copy(sourcePath, destinationPath);
            //--End 1--//
                        
            string productKey = "Ali";
            string userName = "Mujtaba";

            string filePath = @"C:\\Temporary\\ConfigurationFile.ini";
            var text = new StringBuilder();
            var text2 = new StringBuilder();

            foreach (string s in File.ReadAllLines(filePath))
            {
                text.AppendLine(s.Replace("PID=ProductKey", "PID=" + "\"" + productKey + "\""));
            }

            using (var file = new StreamWriter(File.Create(filePath)))
            {
                file.Write(text.ToString());
            }

            foreach (string s in File.ReadAllLines(filePath))
            {
                text2.AppendLine(s.Replace("SQLSYSADMINACCOUNTS=User", "SQLSYSADMINACCOUNTS=" + "\"" + userName + "\""));
            }

            using (var file = new StreamWriter(File.Create(filePath)))
            {                
                file.Write(text2.ToString());
            }
            

        }


What I have tried:

For instance:
" PID=ProductKey"

Here PID is constant and ProductKey can be anything. For now I have hard coded ProductKey and tried the below code which is working.
But when the ProductKey changes my code won't work.

Please Help!!
Posted
Updated 21-Aug-16 21:58pm
Comments
Anisuzzaman Sumon 22-Aug-16 3:01am    
Is your product key has constant length ? Give your product key pattern and what character ends with your "PID=ProductKey" .Give some real data of 's' so that we can figure out and help you.
Member 11579819 22-Aug-16 3:02am    
Product key format: 27HMJ-GH7P9-X2TTB-WPHQC-RG79R
Anisuzzaman Sumon 22-Aug-16 3:04am    
ok then wait :)
Member 11579819 22-Aug-16 3:05am    
ok..thanks
Member 11579819 22-Aug-16 3:18am    
it can also be like replace the entire line which begins with PID...

I'd suggest to start here: Code Project Knowledge Base[^]

You need to use ini reader/parser/writer, for example:
INI Files[^]
Ini Handler[^]
Simplified INI Handling[^]
 
Share this answer
 
You should learn about Regular Expressions (RegEx)

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
 
Share this answer
 
Finally Done..

C#
string filePath = @"C:\\Temporary\\ConfigurationFile.ini";
            
            string[] arrLine = File.ReadAllLines(filePath);
            for (int i = 0; i < arrLine.Count(); i++)
            {
                if (arrLine[i].Contains("PID"))
                {
                    arrLine[i] = "PID=" + "\"" + productKey + "\"";
                }
                if (arrLine[i].Contains("SQLSYSADMINACCOUNTS"))
                {
                    arrLine[i] = "SQLSYSADMINACCOUNTS=" + "\"" + userName + "\"";
                }
            }
            File.WriteAllLines(filePath, arrLine);
 
Share this answer
 
here is your complete solution
C#
string s = "Hello Anis PID=27HMJ-GH7P9-X2TTB-WPHQC-RG79R is a pattern";

           s = Regex.Replace(s, @"(P)(I)(D)(=)[A-Z0-9]{5}(-)[A-Z0-9]{5}(-)[A-Z0-9]{5}(-)[A-Z0-9]{5}(-)[A-Z0-9]{5}","[Your replacement String]", RegexOptions.IgnoreCase);



Out put will be "Hello Anis [Your replacement String] is a pattern" for all matching
pattern like(PID=27HMJ-GH7P9-X2TTB-WPHQC-RG79R)

happy coding :)
 
Share this answer
 

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