Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to hazard a guess here, since you haven't given us enough to really give you a solid answer.</p> <p>I bet the problem is the parsing of that line part, this expression:</p> <pre><code>int.Parse(line.Split(',')[0]) </code></pre> <p>That this doesn't, in all cases, actually give you a number.</p> <p>So you should look at the file, and possibly change the code to this:</p> <pre><code>string[] parts = line.Split(','); ... int.Parse(parts[0]) ... </code></pre> <p>Then you can inspect the contents of that array to see what is really in that first column.</p> <p>As noted in the comments, a better approach would be to not blindly assume the parsing will succeed, and additionally you would probably want to ensure you're using the right cultural information.</p> <p>Here's what I would do, assuming the numbers are output by a program, for reading into another program:</p> <pre><code>int value; if (Int32.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) ... your code here, use value where you need it else what to do in the case of unable to parse the string as a number </code></pre> <p>On a hunch, do you need to trim the contents? Try this simple change:</p> <pre><code>... int.Parse(line.Split(',')[0].Trim()) ... ^--+--^ | +-- add this </code></pre> <p>Other tips:</p> <ul> <li><p>Use <code>Path.Combine</code> to combine paths:</p> <pre><code>new FileStream(Path.Combine(installPath, "storedUsers.txt") </code></pre></li> <li><p>Rewrite your reading loop to this:</p> <pre><code>string line; while ((line = userStream.ReadLine()) != null) { ... } </code></pre></li> <li><p>Use the <code>using</code> clause for disposing of your stream:</p> <pre><code>using (FileStream userFile = new FileStream(...)) { ... } // delete the .Close() line, this is handled by the } on the preceeding line </code></pre></li> </ul> <p>Final version:</p> <pre><code>installPath = @"C:\Program Files (x86)\MyApp\"; using (FileStream userFile = new FileStream( Path.Combine(installPath, "storedUsers.txt"), FileMode.OpenOrCreate, FileAccess.Read)) using (StreamReader userStream = new StreamReader(userFile)) { string line; while ((line = userStream.ReadLine()) != null) { string[] parts = line.Split(','); int userId; if (Int32.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out userId)) storedUsers.Add(userId); } } </code></pre> <p>Of course, if the file isn't too big, you can rewrite the whole shebang using a bit of LINQ:</p> <pre><code>// warning, LINQ with side-effects, evaluate here, don't use it lazily int tempUserId = 0; List&lt;int&gt; userIds = File .ReadAllLines(Path.Combine(installPath, "storedUsers.txt")) .Select(l =&gt; l.Split(',')[0]) .Where(p0 =&gt; Int32.TryParse(p0, NumberStyles.Integer, CultureInfo.InvariantCulture, out tempUserId)) .Select(dummy =&gt; tempUserId) .ToList(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload