locked
How to perform this... RRS feed

  • Question

  • Hi folks,

    I have a ComboBox that list language names (English, French, Spanish ...etc).  When I select a language I want it to search though a directory and see if it exists.  The filenames are saved like this: Application.fr-FR.resx, Application.ja-JP.resx, Application.en-GB.resx...etc

    So if I select French it'll search for filenames containing "fr-FR" in them.  There should be only one.
    How can I perform this please?

    // search folder subdirectories for *.resx
    String[] files = Directory.GetFiles(@"C:\Files\01", "*.resx", SearchOption.AllDirectories);

    foreach (string file in files) // loops through each file Path
                {
                    string fileName = Path.GetFileName(file); 
                    // I some how need to save the path somewhere of the current comboBox1.SelectedItem...
                   // Associate French from the ComboBox with the single "Application.fr-FR.resx" file
                  // I assume I'll have to covert the FileName to a string & then search for fr-FR in it?
                }

    Can this be done with regular expressions?
    foreach (string file in files) {
    string fileName = Path.GetFullPath(file);
        for (int i = 0; i < fileName.Length; i++)
           
    if (Regex.IsMatch(fileName[ i ], @"\d{*}.\d{4}")) ... this does not seem to be correct?

    TIA

    Thursday, November 16, 2006 12:43 PM

Answers


  • If you have a 1:1 mapping from a specific culture name to a concrete filename (as it seems), then you don't actually need to perform any search.

    Assuming that you have a ComboBox comboBox1 somewhere, you could initialize it with a list of languages like this:

      using System.Globalization;
      using System.IO;

      ...

      CultureInfo[] supportedCultures = new CultureInfo[] { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("de-DE") };
      // Or like this
      // CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

      foreach (CultureInfo ci in supportedCultures)
        comboBox1.Items.Add(ci);
        
     Then, in your event handler for the comboBox1.SelectionChangeCommitted event
     
       // this.comboBox1.SelectionChangeCommitted += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
     
     you could simply pick the currently selected culture and look for the corresponding file:
     
      private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
      {
        CultureInfo ci = comboBox1.SelectedItem as CultureInfo;
        string resourceFilename = "Application." + ci.Name + ".resx";
        if (File.Exists(resourceFilename))
          // do something with the file
        ...
      }
     
    Friday, November 17, 2006 1:18 PM

All replies

  •  

    Not sure you are going the right way around your Language issue .. but this is probably what you are looking for.

     

    DirectoryInfo dir = new DirectoryInfo(pathTodirectory);

    string extensionLooking4 = ".fr-Fr.";

    foreach (FileInfo fi in dir.GetFiles())

    {

    if (fi.Extension.ToLower() == ".resx")

    {

    if (fi.Name.ToLower().IndexOf(extensionLooking4) != -1)

    {

    Console.WriteLine("That would be the file " + fi.Name);

    }

    }

    }

    Thursday, November 16, 2006 6:56 PM
  • Thanks ...

     James Knowles wrote:

    Not sure you are going the right way around your Language issue ..


    Have you got any other idea's of what would be a better way of doing this?
    Friday, November 17, 2006 9:59 AM

  • If you have a 1:1 mapping from a specific culture name to a concrete filename (as it seems), then you don't actually need to perform any search.

    Assuming that you have a ComboBox comboBox1 somewhere, you could initialize it with a list of languages like this:

      using System.Globalization;
      using System.IO;

      ...

      CultureInfo[] supportedCultures = new CultureInfo[] { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("de-DE") };
      // Or like this
      // CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

      foreach (CultureInfo ci in supportedCultures)
        comboBox1.Items.Add(ci);
        
     Then, in your event handler for the comboBox1.SelectionChangeCommitted event
     
       // this.comboBox1.SelectionChangeCommitted += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
     
     you could simply pick the currently selected culture and look for the corresponding file:
     
      private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
      {
        CultureInfo ci = comboBox1.SelectedItem as CultureInfo;
        string resourceFilename = "Application." + ci.Name + ".resx";
        if (File.Exists(resourceFilename))
          // do something with the file
        ...
      }
     
    Friday, November 17, 2006 1:18 PM
  • Thanks Vladimir I'll look into that it seems easier.
    Monday, November 20, 2006 12:50 PM