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><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/
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.
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