ashishware.com
ashishware.com RSS Feed
 

.NET, Linux and File Paths



Recently, I ran into a small problem while trying my Image Resize utility on Linux. I have written it using C# (.NET) and tested it on windows, everything works fine. On Linux (with Mono) it gave problems.  Having programmed on windows, I have always assumed that file paths have this form

C:\MyFolder\Myfile.txt

So, this line of  code works fine for Windows:

outpath = destdirname + @”" +newFileName;

But on Linux, it does not give desired output.  In Unix/Linux, ‘/’ is used as directory separator and interestingly, ” can be part of a file name!  So, what I did seems like a really bad coding practice. The best way to do this is:

outpath = destdirname + Path.DirectorySeparatorChar  + newFileName;

There is a nice article on MSDN regarding this. So next time I write .NET code, I better remmember, it has to run on Linux also .

Comments are closed.