Friday 12 August 2011

SaveFileDialog bug under Mono .NET in KDE

Found a bug... in Mono combined with KDE.  So, today I released a little internal tool on Windows, some of our department run Linux, and the application is writtein in C#, so I figured I'd just cross check it on Mono and release it as a Mono flavour executable too.

All seemed fine until the very last step, where the generated output is saved.  The application pops up a "SaveFileDialog" a very simple, standard System.Windows.Forms dialog, the code shows this and used the resulting Filename to save some data.

Under Windows on Microsoft .net Framework 3.5 it was fine, Under Mono within Gnome it was fine.  However, under Mono on KDE I was getting a really strange result, it kept crashing in a routine (wholly unrelated to the actual issue I found) because the file name being returned under KDE was not lead with a directory separator character (as required in Linux).

So, the path under windows might come back as "C:\Data\File.txt" in Ubuntu it comes back as "/home/data/file.txt" but under KDE it was coming back as "home/data/file.txt".  Meaning that the data files were being placed in the wrong location, they were going into a path relative to the applications runtime path.

Meaning the next time I ran the application, from a different root directory or as a different user, or from a folder which did not allow file creation, I could not find my data, I got undefined program behaviour and basically data-wise everything went pear shaped.

I solved this with a simple check:


private void SaveFile (string psFilename)
{
// Save the file
}


private void PathExample(string psThePath)
{
string lsPath = psThePath;
#ifndef WIN32
if ( !lsPath.StartsWith ("" + Path.DirectorySeparatorChar) )
{
lsPath = Path.DirectorySeparatorChar + lsPath;
}
#endif
SaveFile (lsPath);
}

public void SaveDataAs ()
{
SaveFileDialog lsfdDialog = new SaveFileDialog();
if ( lsfd.ShowDialog() == DialogResult.OK )
{
PathExample(lsfdDialog.Filename);
}
lsfdDialog.Dispose();
}

So, this is a problem with the current (as of writing) Mono on KDE, under Kubuntu 11.04, don't fall foul of it.

No comments:

Post a Comment