Cross-platform filesystem navigation in Mono

I am wondering, how do you compensate for different file structures between Linux and Windows when programming for a cross-platform application?

For example, if I want to scan my /home directory in Linux, and my “My Documents” in Windows with something made in Mono (C# or VB.NET)?

Or scanning a network folder in Linux (smb://<server>/<folder>) as opposed to Windows (\<server>&lt;folder>).

Or even just that Windows uses the back slash ("") and Linux uses the forward slash ("/")?

I am not very experienced with client programming (work in ASP.NET) so I am taking Mono as an opportunity to “re-use” my (minuscule) .NET experience.

Thanks.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

windows also works with forward-slashes. That is incorrectly uses
backslashes is what most people know but forward-slashes usually work as
well. Also I’d be surprised if you needed to handle this type of thing on
your own without a system property indicating a path delimiter that you
could then use thanks to the framework.

Good luck.

On 08/04/2010 10:06 AM, dragonbite wrote:
>
> I am wondering, how do you compensate for different file structures
> between Linux and Windows when programming for a cross-platform
> application?
>
> For example, if I want to scan my /home directory in Linux, and my “My
> Documents” in Windows with something made in Mono (C# or VB.NET)?
>
> Or scanning a network folder in Linux (smb://<server>/<folder>) as
> opposed to Windows (\<server>&lt;folder>).
>
> Or even just that Windows uses the back slash ("") and Linux uses the
> forward slash ("/")?
>
> I am not very experienced with client programming (work in ASP.NET) so
> I am taking Mono as an opportunity to “re-use” my (minuscule) .NET
> experience.
>
> Thanks.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJMWZFMAAoJEF+XTK08PnB5tXsP/06BRfpNMx0ofD/qqkVTfiD2
aJXpoHShnSjW4MVOQW9h9Xovez82LgySEX5PTKUpZ2hzI5Io5IPwDjT6jiiqJKGB
r4ckBUzZNk8kS3HVqpFe1yNCEJasSu80J2qQwzGWY6t2r7tSC1Lo22a3ycUY8YIl
8G12qU2tF5FdIuQ2qNOiw/wioz9L+gwUDLckbU5FHhd+UyMJCjFXQlu0rQrc4g0p
YCn1vrD9XcYqkn1+SjMvCSOxBdSUyUJtCO0xDQAJZz/rRZbewMHqrK6pJGKfww2M
TVYj+krWYwF5nZIJJHiYn/MM58Tn1wQoH2VgByPlwEEIdWARNJfhTzR3ppGpqvOp
UffmYmSM+ttlJIcT+5PAThPEhZgP6R0p3EEcjknMib7YsuhIgAcU7VvbHzRkozv9
MdZFUluoqgmwByMv8w6n7f1fm0zu7l8ry0V+NE0vjzA7AFbP6Bb5X1Ptf8M+r2rr
dxVgt70O6vsZBUBtDQ1wXaBtWIWnOG7pLIzVSkvBF/JH5Deu9bv1YPgE5gs5yyg5
7Vd9GLbLvh9vjN/j9nOONuQbz1sQj/styoLz9uHy2/YThaSdva1J0Qkz8G9gF9Oz
iVYV9l6yYe8M8LuasPVH6E99sc78UscWLcfD+eKVBk37F1jXPZor7fCAATGlPKJ6
sWDvFMpLXl7P7n3N7F8K
=3RJz
-----END PGP SIGNATURE-----

I got a response elsewhere too.

I don’t know much about .NET, although in other cross-platform systems like Python, you compensate for this by using a set of library functions, such as those in os.path:

# joining a path:
os.path.join("home", "user", "bin")

# -> on posix
"/home/user/bin"

# -> on windows
'C:\home\user\bin'

You should investigate .NET’s equivalent of this library (I’m sure it has one…well…it should have one) which might have functions for doing what you’re talking about. If it doesn’t, you might have to go ahead and roll your own system, which would involve detecting which OS you’re running on and tailoring the directories accordingly.

With somebody else following up on that response.

Look at the members of System.IO.Path, particularly Path.DirectorySeparatorChar, and Path.Join ().

The MSDN docs are very thorough and complete, and will apply equally to Mono on Linux/OSX as they do to .NET on Windows.

Path Class (System.IO)

Does anybody have any experience with using this, or some other method?

Mono is the opensource implementation of .net correct? So, stuff written in .net should also work (to an extent) under mono?

In .net (which is what I do at work) if I want to save/read something in a user’s Documents folder (equivalent of ~ probably), I would do:
my.computer.filesystem.specialdirectories
The my.computer.filesystem namespace has lots of useful things in, worth poking there.

The Type System.PlatformID might be useful as well (should tell you an OS), as would checking if a System.PlatformNotSupportedException is raised when you test your code.

For building paths you can do:
declare an array of strings: (home,bin,bar,foo)
then string.join(system.io.path.DirectorySeparatorChar,array)
on windows home\bin\bar\foo
unix home/bin/bar/foo