Simple StreamReader issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaeldebruin
    New Member
    • Feb 2011
    • 134

    Simple StreamReader issue

    I am trying to use a text file to save the language my program needs to use. And saving the language in the text file goes fine. Also retrieving the language from it goes fine. But using the retrieved string to catch a language from it won't succeed. And somehow it won't work no matter what I try.

    This is my code:

    Code:
    public static string language = "";
    
            private void ExtensionSearcher_Load(object sender, EventArgs e)
            {
                try
                {
                    if (File.Exists("setup.txt"))
                    {
                        StreamReader sr = new StreamReader("setup.txt");
    
                        language = sr.ReadToEnd();
    
                        MessageBox.Show(language);
    
                        switch (language)
                        {
                            case "Nederlands":
                                lblSearchExt.Text = "Zoek op extensie";
                                lblSearchType.Text = "Zoek op type";
                                lblSearchProgram.Text = "Zoek op programma";
                                lblExtList.Text = "Extensies";
                                lblTypeList.Text = "Types";
                                lblProgramList.Text = "Programma's";
                                break;
                            case "English":
                                lblSearchExt.Text = "Search by extension";
                                lblSearchType.Text = "Search by type";
                                lblSearchProgram.Text = "Search by program";
                                lblExtList.Text = "Extensions";
                                lblTypeList.Text = "Types";
                                lblProgramList.Text = "Programs";
                                break;
                        }
                    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Are you saying none of your cases are hit?

    Can you please post the contents (or at least, the relevant contents) of setup.txt?

    Comment

    • michaeldebruin
      New Member
      • Feb 2011
      • 134

      #3
      ow I see now, sorry that I didn't explain the issue. But yes idd none of the cases are hit. When using simple installer, to write the language in a text file and in this case English or Nederlands. None of the cases are hit after the using the streamreader.

      The only thing which is writtin in the file is English or Nederlands.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Oh! Inspect the contents of language directly. Output the length if you have to, or even inspect the ASCII codes. I'd be willing to bet that when you do a ReadToEnd there you're picking up a trailing newline, or perhaps some other end of file type character.

        Take a look :)

        Comment

        • michaeldebruin
          New Member
          • Feb 2011
          • 134

          #5
          Do you think a ReadLine would do the trick? Or use a special character like / to split the string which I want to retrieve from the other stuff?

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            You could try any number of things. A readline would likely work, or a string.Trim. You could also store it in XML.

            Any number of things really :) Just verify the contents of your string using the debugger if your compares aren't working. That's usually a good starting point.

            Comment

            • michaeldebruin
              New Member
              • Feb 2011
              • 134

              #7
              Oke well thanks for all the help. I think it can be done way easier. But now I've added a special character to it (in the case that I am going to write some more in the text file). And then used a string.trim to get the language from it. And then just added a if statement to say that when the language is English it is English and for some reason the case of English got hit. And the same is for the case Nederlands (or Dutch). I don't know what the real issue was, but it finally works :).

              Comment

              Working...