Use a text file as a resource file in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nelsonbrodyk
    New Member
    • Mar 2008
    • 81

    Use a text file as a resource file in C#

    Hey all,
    I am trying to separate my translation text from my application. To do this, I would like to use resources, but as a text file. I saw that you can create a text file that looks like the following:

    MyKey=MyText
    MyKey2=MyText2

    etc.

    What I am wondering is, is there a nice way to access this file and get it by the key, like properties files in java?

    I tried this, but it doesn't seem to work:
    ResourceManager manager1 = ResourceManager .CreateFileBase dResourceManage r("TestResource s.en-US.txt", loc, typeof(string)) ;
    Console.WriteLi ne("Reached: {0}", manager1.GetStr ing("MyTestReso urce"));

    Thanks in advance.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Originally posted by nelsonbrodyk
    Hey all,
    I am trying to separate my translation text from my application. To do this, I would like to use resources, but as a text file. I saw that you can create a text file that looks like the following:

    MyKey=MyText
    MyKey2=MyText2

    etc.

    What I am wondering is, is there a nice way to access this file and get it by the key, like properties files in java?

    I tried this, but it doesn't seem to work:
    ResourceManager manager1 = ResourceManager .CreateFileBase dResourceManage r("TestResource s.en-US.txt", loc, typeof(string)) ;
    Console.WriteLi ne("Reached: {0}", manager1.GetStr ing("MyTestReso urce"));

    Thanks in advance.
    Try Settings. If you are using a Windows Forms solution in Visual Studio, look in your project's Properties file. There will be a file called Settings.settin gs
    If you open it, you can create keys and set default values. You can also set whether these are user-level or application-level settings.

    You can get to this from your code with:
    [CODE=cpp]
    Properties.Sett ings.Default["keyname"];
    [/CODE]

    For that matter, it doesn't even have to be a Windows Forms project. Even in a console application you can add a Settings.settin gs file and move it to the properties folder.
    Last edited by Curtis Rutland; Apr 11 '08, 06:56 PM. Reason: remembered something

    Comment

    • nelsonbrodyk
      New Member
      • Mar 2008
      • 81

      #3
      Originally posted by insertAlias
      Try Settings. If you are using a Windows Forms solution in Visual Studio, look in your project's Properties file. There will be a file called Settings.settin gs
      If you open it, you can create keys and set default values. You can also set whether these are user-level or application-level settings.

      You can get to this from your code with:
      [CODE=cpp]
      Properties.Sett ings.Default["keyname"];
      [/CODE]
      For the translations, we want to allow them to be updatable outside of compile time. Ie, so that someone can edit the translations, and the application just uses them. Is there a way to basically do a properties file (or ResourceBundle in Java), that will achieve this?

      Ideally, I would like to have a file called: Translations.tx t, and then use the resource manager to handle it, and allow me to use ResourceManager .GetString("Key ");

      Is this possible with resources, or should I be looking else where? My understanding is the Settings file isn't updatable after compiling?

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Originally posted by nelsonbrodyk
        For the translations, we want to allow them to be updatable outside of compile time. Ie, so that someone can edit the translations, and the application just uses them. Is there a way to basically do a properties file (or ResourceBundle in Java), that will achieve this?

        Ideally, I would like to have a file called: Translations.tx t, and then use the resource manager to handle it, and allow me to use ResourceManager .GetString("Key ");

        Is this possible with resources, or should I be looking else where? My understanding is the Settings file isn't updatable after compiling?
        It actually is updateable. If you look in your bin\(debug/release) folder, you should see an exe file. Also, there is a .config file with the same name as the .exe. That config file is an xml file that holds the keys that you defined. So, if you just want to be able to edit the keys with a text editor after compile, you still can. Also, if you are making a windows installer package, it should install both the exe and the .config file, so you can make configuration changes there.

        Maybe not quite what you were looking for, but that's how I know how to do it.

        Comment

        • nelsonbrodyk
          New Member
          • Mar 2008
          • 81

          #5
          Originally posted by insertAlias
          It actually is updateable. If you look in your bin\(debug/release) folder, you should see an exe file. Also, there is a .config file with the same name as the .exe. That config file is an xml file that holds the keys that you defined. So, if you just want to be able to edit the keys with a text editor after compile, you still can. Also, if you are making a windows installer package, it should install both the exe and the .config file, so you can make configuration changes there.

          Maybe not quite what you were looking for, but that's how I know how to do it.
          Will that still give the ability to change the language? With the resource files, you can have english, french, etc easily.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Originally posted by nelsonbrodyk
            Will that still give the ability to change the language? With the resource files, you can have english, french, etc easily.
            Hmm. Tough one. I haven't ever had to work with translations, so I guess I was missing the point. So I'm not really sure. But if you just want to set up a file with keys and values, and then edit those values later with a text editor, then yes this will work.

            Comment

            Working...