-2

in c# i need to compare 2 numbers one from a local file, and one another from a downloaded file like a Patcher.

if I use Streamreader c# sad to me that he can't convert string into INT.

are there a solution for this? file a contains the value "1" , the file b contains the value "2"

so if b>a then download the new files catch from another updater file.

thanks

  • 1
    How did you try to convert to int ? Did you look at the string? Maybe there are control characters included? – malte May 17 '14 at 19:47
  • Can you show the code that you are using for reading the number? And include the exact contents of one of the files? – O. R. Mapper May 17 '14 at 19:47

3 Answers3

2

If that is the only number in the file, you can use File.ReadAllText (or File.ReadAllLines in a multiline file) and convert to int like this:

string[] lines = File.ReadAllLines(@"c:\t.txt");
int number = Convert.ToInt32(lines[0]);
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

try to use the Convert.ToInt32 method. If your file contains olny one number, you could use the File.ReadAllLine method, insted of streamreader.

Tamás Varga
  • 635
  • 5
  • 17
-1
void CompareVersions()
{
    WebClient client = new WebClient();
    var serverVersion = client.DownloadString("http://yourwebsite.com/version.txt");

    using (StreamReader sr = new StreamReader("file.txt"))
    {
        if (Convert.ToInt32(serverVersion) > Convert.ToInt32(sr.ReadLine()))
        {
            // server version bigger
        }
        else
        {
            // up to date
        }
    }
}
David
  • 73
  • 1
  • 3
  • 10
  • tks, just tested. this solution take me out managing two files – Simone Bezze May 17 '14 at 20:08
  • 2
    An answer with code for copy and pasting without any explanations may help the OP in their current question, but probably does not help them understand how this problem, and similar problems, can be solved. This is even more true for future visitors with similar, but not the same problem, who cannot benefit from a copy-and-paste answer at all. – O. R. Mapper May 17 '14 at 20:18
  • @David/ update your answer to include explanation and you might reverse your downvotes since I think it is a decent piece of code. – Patrick Hofman May 18 '14 at 06:35