0

iam creating an app for a handheld with windows embedded handheld 6.5, iam stuck with reading the content of text file and loading into a string. the file is tab delimeted with 3 columns (barcode, desc, price).the code will be executed on form load, i have written the below using c# on vb2005 releasing on windows 6 classic emulator, but the streamreader is always null. i have written the below please any advices, i appreciate the help!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

private void Form1_Load(object sender, EventArgs e)
{
    string fileloc = "fileexample.txt";               
    StreamReader sr = new StreamReader(fileloc);
    string s = sr.ReadToEnd();                   
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
ali
  • 31
  • 4
  • Q: Have you tried [File.ReadAllText()](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=net-6.0)? – paulsm4 Dec 09 '21 at 00:22
  • When i type File. I do not have the read all text option – ali Dec 09 '21 at 00:49

4 Answers4

1

OK:

How can I read all text when ReadAllText is unavailable?

The compact-framework edition does not implement it. The .net version, however, is simply implemented like this:

public static class File
{
    public static String ReadAllText(String path)
    {
        using (var sr = new StreamReader(path, Encoding.UTF8))
        {
            return sr.ReadToEnd();
        }
    }
}

Note the second argument to StreamReader: Encoding.UTF8.

See also:

Compact Framework: A problem with reading a file

Windows CE doesn't have the concept of "current directory". The OS tries to open \list.txt when passing "list.txt". You always have to specify the full path to the file. ...

In full framework I use:

string dir = Path.GetDirectory(Assembly.GetExecutingAssembly().Location);
string filename = Path.Combine(dir, "list.txt");
StreamReader str = new StreamReader(filename);

STRONG SUGGESTIONS:

  1. Specify an encoding (e.g. UTF8)
  2. Set a breakpoint in this method and single-step through your code.
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • thank you when i compile iam getting the below error: – ali Dec 09 '21 at 11:12
  • Error 2 The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) C:\Users\ali\Documents\Visual Studio 2005\Projects\price_check_c\price_check_c\Form1.cs 19 20 price_check_c – ali Dec 09 '21 at 11:12
  • 1
    "var" is a keyword that was introduced in C# 3.0: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var. Your version is apparently too old. Just substitute `StreamReader` for "var". ALSO: please "upvote" and "accept" (the "check" icon) whenever you find a response helpful. – paulsm4 Dec 09 '21 at 16:29
  • i have a motorola 3190 running on CE OS 5.2, iam using VS2005..dot net compact v2..iam aware that var and File.* are not all avilable in this version, but i am searching an alternative i couldnt get one, i just need to import the contents of .txt file into a string or into an array in c# – ali Dec 09 '21 at 19:02
  • 1
    The compiler version (e.g. MSVS 2005) is less relevant than your target platform. Your target doesn't support "var" - the workaround is to explicitly give the type (StreamReader). Your target *should* support [System.IO.StreamReader](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader). The code above *should* work if you just substitute "var". Please post back specific messages if it doesn't. – paulsm4 Dec 09 '21 at 20:01
0

Try use

File.ReadAllText("Full File Path");

and use the Full file path like C:\fileexample.txt

0

After a long search, I found what I was looking for. I wanted to share so others would not suffer like I did:

private void Form1_Load(object sender, EventArgs e)
{
    string dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
    string filename = Path.Combine(dir, "alisyd.txt");
    StreamReader sr = new StreamReader(filename);
    string s = sr.ReadToEnd();          
}
paulsm4
  • 114,292
  • 17
  • 138
  • 190
ali
  • 31
  • 4
  • Your response is substantially what I said in the first place: 1) you need to specify a fully qualified path, 2) You need sr.ReadToEnd(); – paulsm4 Dec 12 '21 at 02:15
0
string FilePath = "/"; //Root
string FileName = "MyFile.txt";
string MyString = "";
StreamReader sr = new StreamReader(File.OpenRead(FilePath + FileName), Encoding.Default, true);
while (!sr.EndOfStream)
{
    MyString += sr.ReadLine();
}
sr.Dispose();
sr.Close();
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77