[Mono-dev] Read file under Linux?

Rafael Teixeira monoman at gmail.com
Wed Aug 25 12:24:58 EDT 2010


Besides what Alex said you need a way to find the file.

You may try to use Assembly.Codebase with Assembly.GetEntryAssembly()
to first locate where your executable is, then use
System.Io.Path.Combine() to compose the full path to your properties
file.

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly.aspx
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.codebase.aspx
http://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx

Fun,

Rafael "Monoman" Teixeira
---------------------------------------
"We live in a world operated by science and technology. We have also
arranged things so that almost no one understands science and
technology. This is a prescription for disaster. We might get away
with it for a while, but sooner or later this combustible mixture of
ignorance and power is going to blow up in our faces."
-Carl Sagan



On Thu, Aug 5, 2010 at 2:39 PM, SamHellawell <samlikeswilly at live.co.uk> 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
>
> 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]);
>                    }
>
>                return true;
>            }
>            catch
>            {
>                Console.WriteLine("Unable to load properties file: " +
> filename);
>                return false;
>            }
>        }
>
>
> Cheers, Sam.
> --
> View this message in context: http://mono.1490590.n4.nabble.com/Read-file-under-Linux-tp2315366p2315366.html
> Sent from the Mono - Dev mailing list archive at Nabble.com.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>


More information about the Mono-devel-list mailing list