Click here to Skip to main content
15,559,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,
I do a text search in in file in the WPF application, which I would then rewrite. I'm completely confused about what to write as a rewritten value. The problem is that the found lines are overwritten, but depending on the number of them in the document, the whole text is copied many times.

I will be very happy for any advice or solution
Thank you!!!

What I have tried:

C#
private void Grid_Loaded(object sender, RoutedEventArgs e)
       {
           TextBox.Focus();
       }

       private void HledatButton_Click(object sender, RoutedEventArgs e)
       {
           string file = TextBox.Text;
           List<string> files = Directory.GetFiles(file, "*.csproj", SearchOption.AllDirectories).ToList();
           foreach (string pathFile in files)
           {
               string[] lines = File.ReadAllLines(pathFile);
               var newPath = "C:\\ArcGISPro\\bin\\";
               var path = @"C:\Program Files\ArcGIS\Pro\bin\";
               List<string> updateLines = new List<string>();
               for (int index = 0; index < lines.Length; index++)
               {
                   string line = lines[index];
                   if (line.Contains(path))
                   {
                       foreach (string s in lines)
                       {

                           line = s.Replace(path, newPath);
                           updateLines.Add(line);
                       }
                   }
                   File.WriteAllLines(newPath, updateLines);
               }
           }
           MessageBox.Show("DONE!");
       }
Posted
Updated 8-Nov-21 0:26am
v3
Comments
Richard Deeming 9-Nov-21 5:52am    
REPOST
This is the same question you posted last week, which you marked as "solved":
How to overwrite a line in a file?[^]
dejf111 9-Nov-21 6:37am    
If you read it better, it's a question of something else.
Richard Deeming 9-Nov-21 6:39am    
It's exactly the same: you're changing lines in a file. In both questions, you're overwriting the file every time you find a matching line, rather than when you've finished processing the file.

The only difference is that you're now processing multiple files instead of just one.
dejf111 9-Nov-21 6:45am    
I mentioned that you must know best, I'm sorry :)

1 solution

That's not going to work.
I'm not at all sure what you expect it to do, but it's not going to do it.
C#
for (int index = 0; index < lines.Length; index++)
{
    string line = lines[index];
    if (line.Contains(path))
    {
        foreach (string s in lines)
        {

            line = s.Replace(path, newPath);
        }
    }
    File.WriteAllLines(newPath, lines);
}

1) You loop through all lines
2) Inside that loop, if the string is found, you loop through all lines again
3) Inside that loop, you set the same variable to many different values.
4) You ignore the value you changed and write the original input out to the new file.
5) The path you output to isn't a file, but a folder.

Many things just look weird here: which probably means you leapt straight into code without thinking about what your task was and the best way to manage or implement it.

I'd strongly suggest that you start again: think about exactly what you need to do, and work out what the code flow should be before you start coding - because whatever your task is, that isn't a good solution!
 
Share this answer
 
Comments
dejf111 8-Nov-21 2:26am    
I expect it to read all the folders in the file that have a .csproj extension. Subsequently, it will find the searched text in the individual folders, which it will replace with my new text. He then writes the corrected one back to that folder.
OriginalGriff 8-Nov-21 2:45am    
So think about the operations that need to be done to do that: as I siad, the existing code won't do that, and looks like it was thrown together without much advance planning.

But the real question is why are you doing that at all? Duplicating projects isn't a good way to do anything as it quickly leads to configuration nightmares when changes occur, particularly if you try to overwrite existing code as it may not even get compiled into other projects because the files dates will be wrong. If you are trying to reuse code, then do it via the VS mechanism(s): references, and existing projects. They handle it for you.
dejf111 8-Nov-21 3:03am    
I've already figured it out but thank you for the advice
dejf111 8-Nov-21 3:45am    
Modified question, new error. Now the text is copied as many times as the text contains the search term.
OriginalGriff 8-Nov-21 4:10am    
And why do you think that is?
Have you tried the debugger yet?

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