简体   繁体   中英

Code for Searching Texts in an external C# file

We have this.label1.Text = "Code Smart"; in our Designer.cs file

Now I am reading all of my .Designer.cs files kept in my solution folder.

Now I want to match these conditions:

  1. If within the string .Text= or .Text = matches or not.
  2. I want to get the text from that string which is in "" (Double Quotes) It means: Code Smart

The idea behind is to collect all the .Text data in a file, so if I get the data matched and Value of that text then I'll export it to a CSV for the translation.

Can someone help me doing this. pls

ADD1

I have done some changes in ADD1 and have revised to ADD2, kindly check and comment. Thanks

ADD2:

Ok finally I reached to this let me know how to get the string within double quotes:

FolderBrowserDialog fbd = new FolderBrowserDialog();

foreach (string file in Directory.EnumerateFiles(fbd.SelectedPath, "*.Designer.cs"))
{
    int counter = 0;
    string line;

    // Read the file and display it line by line.
    StreamReader CurrentFile = new StreamReader(file);
    while ((line = CurrentFile.ReadLine()) != null)
    {
        bool typeOne = line.Contains(".Text=");
        bool typeTwo = line.Contains(".Text =");
        if ((typeOne == true) || (typeTwo == true))
        {
            // Get the data which is enclosed in double quotes
        }
        counter++;
    }
    CurrentFile.Close();
}

then I'll export it to a CSV for the translation.

If the goal is to have a multilingual GUI, I don't recommend doing it this way. Try to use etither Zeta Resource Editor , or Resx Resource Translator .

Check this resource as well: Walkthrough: Localizing Windows Forms . It describes how to create multiple language resource files that you can edit with either Zeta or Resx.

Hope this is what you tried to achive. If not, igonre this answer.

Assuming there's no nesting of double quotes, you might use this regex:

\.Text\s*=\s*"([^"]+)"

ideone demo

See if this helps;

foreach (string file in Directory.EnumerateFiles(fbd.SelectedPath, "*.Designer.cs"))
{
    int counter = 0;
    string line;
    Regex r = new Regex(@"this.label1.Text[ ]*[\=]{1}[ ]*[\""]{1}(?<StringValue>[a-zA-Z0-9 ]*)[\""]{1}[ ]*[\;]{1}");
    // Read the file and display it line by line.
    StreamReader CurrentFile = new StreamReader(file);
    while ((line = CurrentFile.ReadLine()) != null)
    {
        bool typeOne = line.Contains(".Text=");
        bool typeTwo = line.Contains(".Text =");
        if ((typeOne == true) || (typeTwo == true))
        {
          Match m=r.Match(line);
          string thevalueyouneed=m.Groups[1].Value;//This is what you need.
          //Do other things with this value.  
        }
        counter++;
    }
    CurrentFile.Close();
}

The first group is the string you need.

    if ((typeOne == true) || (typeTwo == true))
    {
        int indexOfTheStartOfText = line.IndexOf(".Text", StringComparison.CurrentCulture);

        if (indexOfTheStartOfText != -1)
        {
            int textLength = (typeOne == true) ? ".Text=".Length : ".Text =".Length;
            int StartIndexOfActualText = indexOfTheStartOfText + textLength + "\"".Length;
            int EndIndexOfActualText = line.IndexOf('"', StartIndexOfActualText);

            string myText = line.Substring(StartIndexOfActualText, EndIndexOfActualText - StartIndexOfActualText);

        }
    }

The calculation of StartIndexOfActualText needs to change for typeOne or typeTwo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM