[Mono-dev] Read file under Linux?

Alexander Shulgin alexander.shulgin at yessoftware.com
Wed Aug 25 12:13:07 EDT 2010


On 05.08.2010 20:39, SamHellawell wrote:
>
> Hey there!
>
> Basically, my C# project works perfectly well under Windows, and after
> sorting out some GAC problems it *almost* works on Linux. I am stump however
> as to how to open and read a file under Linux using Mono, i have tried
> entering Linux paths and local paths. The file I wish to read is within the
> same directly as the binary. If anybody could help me out, i'd greatly
> appreciate it. The file i wish to load is called example.properties

Posting the exact exception/error log you get would help greatly to 
identify the problem.

> My load function:
>
>          public Boolean Load(string filename)
>          {
>              try
>              {
>                  FileStream file = File.Open(filename, FileMode.Open);
>
>                  UTF8Encoding temp = new UTF8Encoding(true);
>                  string fileContents = "";
>
>                  byte[] b = new byte[1024];
>                  while (file.Read(b, 0, b.Length)>  0)
>                  {
>                      fileContents += temp.GetString(b);
>                  }
>
>                      string[] lineSplit = fileContents.Split((char)10);
>
>                      for(int i = 0; i<  lineSplit.Length; i++)
>                      {
>                          props.Add(lineSplit[i].Split('=')[0],
> lineSplit[i].Split('=')[1]);
>                      }

There's also File.ReadAllLines() method which does what you're trying to 
accomplish.

--
Alex


More information about the Mono-devel-list mailing list