Posts Tagged ‘C#’

SlashXML

I recently put up the SlashXML library on bitbucket. SlashXML provides a way to retrieve data from an XML document via path names to elements. It’s somewhat in the same vein as XPath, but much simpler. I wrote this way back when because I was annoyed at the more conventional method of having to extract data during the depth-first traversal of the document tree – this is a fine way to parse the document and pull in the data but not necessarily the most elegant way to query it. I haven’t written any documentation so, as I did with the bluetooth stuff, I’ll post a quick-and-dirty tutorial here.

The C# code files and .NET DLL are up now (/cs folder). I do have a version of this in C++ which uses TinyXML, but there’s a major bug that needs to be resolved before I upload it.


Load an XML document from a file

SlashXML.SlashXMLReader xmlReader = new SlashXML.SlashXMLReader();
xmlReader.LoadXMLFile(filepath);


Load an XML document from a string

SlashXML.SlashXMLReader xmlReader = new SlashXML.SlashXMLReader();
xmlReader.LoadXMLString(xmlString);


Retrieve data from an element

string data = xmlReader.GetDataAt("root/child");

If the element doesn’t exist, has no data, or contains other elements instead of data, the return value will be null.


Retrieve data from an indexed element

string data = xmlReader.GetDataAt("root/child[2]");

Indexing on data read in occurs when there are multiple elements with the same name.


Get the number of child nodes under an element

int numChildren = xmlReader.GetNumChildren("root", "child");


Note that underlying SlashXML.SlashXMLReader is SlashXML.StringTreeUtility.StringTree which actually holds the data read in and which can be queried directly for the data. Also, SlashXML reads everything into memory – do not use this for massive files (e.g. 1GB+ documents)! In fact, you should probably reconsider your use of XML if your files are that massive.

You’ll notice a major missing feature here – retrieving data in attributes! It’s not too difficult to do, but I haven’t had time to do it. It’ll be in a future version.

Mono Stream issue

An issue I discovered in Mono. When attempting to write a zero-length byte array (representing an empty string of UTF8 characters) to a HttpWebRequest stream, the stream is cut short at the position the write is being attempted; nothing after it gets written to the stream.

protected void WriteAllToStream(Stream stream, byte[] data)
{
// data.Length needs to be > 0
stream.Write(data, 0, data.Length);
}

I’m not sure if this is specific to the HttpWebRequest stream. I encounter the issue on Mono 2.4 (under Ubuntu) and Mono 2.6.1 (under openSUSE).

File.OpenRead, FileShare issues

The following piece of C# code to open a file for reading (an MS Word document, opened by Word), generated an IOException.

FileStream fs = System.IO.File.OpenRead(oldFullPath);

The IOException:

The process cannot access the file ... because it is being used by another process.

Not too unusual, but curiously, the following code, to do the same same thing, did not generate any exceptions and the file was read successfully.

FileStream fs = System.IO.File.Open(oldFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

I’d always assumed that File.OpenRead() was equivalent to the File.Open() call above.
Bad assumption.
It looks like the FileShare flag is set to FileShare.Read not FileShare.ReadWrite by File.OpenRead().

Note that the FileShare flag is a bit misleading as described by the docs, it doesn’t only apply to subsequent operations. Here’s a good explanation by Darin Dimitrov from stackoverflow:

In the case of future openers, a FileShare.Read means “future openers can open the file for reading”. In the case of past openers, FileShare.Read means “open this file for me only if it has past openers that opened it for reading only”. That’s why FileShare.ReadWrite needs to be used here, because Excel opens the file for writing.

Mono.Unix.Native

After spending some time attempting to P/Invoke the stat() function, I discovered the wonderful Mono.Unix.Native namespace (found in the Mono.Posix assembly) which has some great stuff for lower-level Unix/Linux work.

Here’s what I did to pull a file’s inode number:

static public ulong GetFileInodeNum(string path)
{
    Mono.Unix.Native.Stat statOut =
new Mono.Unix.Native.Stat();
    
int statRet = Mono.Unix.Native.Syscall.stat(path, out statOut);
    
    
if(statRet == -1)
    {
        
throw new IOException("stat on " + path + " failed.");
    }
    
    
return statOut.st_ino;
}

Mono PlatformID on MacOS X

Unfortunately, the method mentioned in the Mono Technical FAQ, where you can check System.Environment.OSVersion.Platform for a value of 6 doesn’t work (as of Mono 2.6). I got the following code from this thread, which works well enough,

[DllImport("libc")]
static extern int uname(IntPtr buf);

static bool IsRunningOnMac()
{
IntPtr buf = IntPtr.Zero;
try
{
buf =
Marshal.AllocHGlobal(8192);
if (uname(buf) == 0)
{
string os = Marshal.PtrToStringAnsi(buf);
if (os == "Darwin") return true;
}
}
catch { }
finally
{
if (buf != IntPtr.Zero) Marshal.FreeHGlobal(buf);
}
return false;
}

MIME types in C#

The .NET Framework has no methods to get the MIME type of a file and, as I was in need of such functionality a while back, I’m glad I found this post on CSharpFriends.com (UPDATE: removed link, CSharpFriends.com is dead) which presents a switch statement that maps numerous filename extensions to the appropiate MIME type.

I was somewhat dismayed that someone mentioned using the system registry to lookup the MIME types, and the OP seemed to have gone with this method. Using the registry cuts down your codebase, but that’s little more than a convenience; there are some important issues that pop up when using the registry in this situation:

  • It only works if the file extension is known to Windows; either the version of Windows the app is running on knows of the file extension or an application puts the file extension into the registry.
  • It binds (read: couples) your app to a third-party component, in a situation where there is no need to.
  • It locks you into a specific platform (i.e. it won’t work with Mono). Even if it’s not a major issue, there’s no reason to lock your app to a certain platform when you don’t have to.

UPDATE: As CSharpFriends.com is dead, the code I derived from the original post is below. The FromFileName() function will take a filepath, get the extension from the filename, and return the MIME type of the file.

static public string FromFileName(string filepath)
{
switch (System.IO.Path.GetExtension(filepath).ToLower())
{
case ".3dm": retval = "x-world/x-3dmf"; break;
case ".3dmf": retval = "x-world/x-3dmf"; break;
case ".a": retval = "application/octet-stream"; break;
case ".aab": retval = "application/x-authorware-bin"; break;
case ".aam": retval = "application/x-authorware-map"; break;
case ".aas": retval = "application/x-authorware-seg"; break;
case ".abc": retval = "text/vnd.abc"; break;
case ".acgi": retval = "text/html"; break;
case ".afl": retval = "video/animaflex"; break;
case ".ai": retval = "application/postscript"; break;
case ".aif": retval = "audio/aiff"; break;
case ".aifc": retval = "audio/aiff"; break;
case ".aiff": retval = "audio/aiff"; break;
case ".aim": retval = "application/x-aim"; break;
case ".aip": retval = "text/x-audiosoft-intra"; break;
case ".ani": retval = "application/x-navi-animation"; break;
case ".aos": retval = "application/x-nokia-9000-communicator-add-on-software"; break;
case ".aps": retval = "application/mime"; break;
case ".arc": retval = "application/octet-stream"; break;
case ".arj": retval = "application/arj"; break;
case ".art": retval = "image/x-jg"; break;
case ".asf": retval = "video/x-ms-asf"; break;
case ".asm": retval = "text/x-asm"; break;
case ".asp": retval = "text/asp"; break;
case ".asx": retval = "video/x-ms-asf"; break;
case ".au": retval = "audio/basic"; break;
case ".avi": retval = "video/avi"; break;
case ".avs": retval = "video/avs-video"; break;
case ".bcpio": retval = "application/x-bcpio"; break;
case ".bin": retval = "application/octet-stream"; break;
case ".bm": retval = "image/bmp"; break;
case ".bmp": retval = "image/bmp"; break;
case ".boo": retval = "application/book"; break;
case ".book": retval = "application/book"; break;
case ".boz": retval = "application/x-bzip2"; break;
case ".bsh": retval = "application/x-bsh"; break;
case ".bz": retval = "application/x-bzip"; break;
case ".bz2": retval = "application/x-bzip2"; break;
case ".c": retval = "text/plain"; break;
case ".c++": retval = "text/plain"; break;
case ".cat": retval = "application/vnd.ms-pki.seccat"; break;
case ".cc": retval = "text/plain"; break;
case ".ccad": retval = "application/clariscad"; break;
case ".cco": retval = "application/x-cocoa"; break;
case ".cdf": retval = "application/cdf"; break;
case ".cer": retval = "application/pkix-cert"; break;
case ".cha": retval = "application/x-chat"; break;
case ".chat": retval = "application/x-chat"; break;
case ".class": retval = "application/java"; break;
case ".com": retval = "application/octet-stream"; break;
case ".conf": retval = "text/plain"; break;
case ".cpio": retval = "application/x-cpio"; break;
case ".cpp": retval = "text/x-c"; break;
case ".cpt": retval = "application/x-cpt"; break;
case ".crl": retval = "application/pkcs-crl"; break;
case ".crt": retval = "application/pkix-cert"; break;
case ".cs": retval = "text/x-csharp"; break;
case ".csh": retval = "application/x-csh"; break;
case ".css": retval = "text/css"; break;
case ".cxx": retval = "text/plain"; break;
case ".dcr": retval = "application/x-director"; break;
case ".deepv": retval = "application/x-deepv"; break;
case ".def": retval = "text/plain"; break;
case ".der": retval = "application/x-x509-ca-cert"; break;
case ".dif": retval = "video/x-dv"; break;
case ".dir": retval = "application/x-director"; break;
case ".dl": retval = "video/dl"; break;
case ".doc": retval = "application/msword"; break;
case ".docx": retval = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break;
case ".dot": retval = "application/msword"; break;
case ".dp": retval = "application/commonground"; break;
case ".drw": retval = "application/drafting"; break;
case ".dump": retval = "application/octet-stream"; break;
case ".dv": retval = "video/x-dv"; break;
case ".dvi": retval = "application/x-dvi"; break;
case ".dwf": retval = "model/vnd.dwf"; break;
case ".dwg": retval = "image/vnd.dwg"; break;
case ".dxf": retval = "image/vnd.dwg"; break;
case ".dxr": retval = "application/x-director"; break;
case ".el": retval = "text/x-script.elisp"; break;
case ".elc": retval = "application/x-elc"; break;
case ".env": retval = "application/x-envoy"; break;
case ".eps": retval = "application/postscript"; break;
case ".es": retval = "application/x-esrehber"; break;
case ".etx": retval = "text/x-setext"; break;
case ".evy": retval = "application/envoy"; break;
case ".exe": retval = "application/octet-stream"; break;
case ".f": retval = "text/plain"; break;
case ".f77": retval = "text/x-fortran"; break;
case ".f90": retval = "text/plain"; break;
case ".fdf": retval = "application/vnd.fdf"; break;
case ".fif": retval = "image/fif"; break;
case ".fli": retval = "video/fli"; break;
case ".flo": retval = "image/florian"; break;
case ".flx": retval = "text/vnd.fmi.flexstor"; break;
case ".fmf": retval = "video/x-atomic3d-feature"; break;
case ".for": retval = "text/x-fortran"; break;
case ".fpx": retval = "image/vnd.fpx"; break;
case ".frl": retval = "application/freeloader"; break;
case ".funk": retval = "audio/make"; break;
case ".g": retval = "text/plain"; break;
case ".g3": retval = "image/g3fax"; break;
case ".gif": retval = "image/gif"; break;
case ".gl": retval = "video/gl"; break;
case ".gsd": retval = "audio/x-gsm"; break;
case ".gsm": retval = "audio/x-gsm"; break;
case ".gsp": retval = "application/x-gsp"; break;
case ".gss": retval = "application/x-gss"; break;
case ".gtar": retval = "application/x-gtar"; break;
case ".gz": retval = "application/x-gzip"; break;
case ".gzip": retval = "application/x-gzip"; break;
case ".h": retval = "text/plain"; break;
case ".hdf": retval = "application/x-hdf"; break;
case ".help": retval = "application/x-helpfile"; break;
case ".hgl": retval = "application/vnd.hp-hpgl"; break;
case ".hh": retval = "text/plain"; break;
case ".hlb": retval = "text/x-script"; break;
case ".hlp": retval = "application/hlp"; break;
case ".hpg": retval = "application/vnd.hp-hpgl"; break;
case ".hpgl": retval = "application/vnd.hp-hpgl"; break;
case ".hqx": retval = "application/binhex"; break;
case ".hta": retval = "application/hta"; break;
case ".htc": retval = "text/x-component"; break;
case ".htm": retval = "text/html"; break;
case ".html": retval = "text/html"; break;
case ".htmls": retval = "text/html"; break;
case ".htt": retval = "text/webviewhtml"; break;
case ".htx": retval = "text/html"; break;
case ".ice": retval = "x-conference/x-cooltalk"; break;
case ".ico": retval = "image/x-icon"; break;
case ".idc": retval = "text/plain"; break;
case ".ief": retval = "image/ief"; break;
case ".iefs": retval = "image/ief"; break;
case ".iges": retval = "application/iges"; break;
case ".igs": retval = "application/iges"; break;
case ".ima": retval = "application/x-ima"; break;
case ".imap": retval = "application/x-httpd-imap"; break;
case ".inf": retval = "application/inf"; break;
case ".ins": retval = "application/x-internett-signup"; break;
case ".ip": retval = "application/x-ip2"; break;
case ".isu": retval = "video/x-isvideo"; break;
case ".it": retval = "audio/it"; break;
case ".iv": retval = "application/x-inventor"; break;
case ".ivr": retval = "i-world/i-vrml"; break;
case ".ivy": retval = "application/x-livescreen"; break;
case ".jam": retval = "audio/x-jam"; break;
case ".jav": retval = "text/plain"; break;
case ".java": retval = "text/plain"; break;
case ".jcm": retval = "application/x-java-commerce"; break;
case ".jfif": retval = "image/jpeg"; break;
case ".jfif-tbnl": retval = "image/jpeg"; break;
case ".jpe": retval = "image/jpeg"; break;
case ".jpeg": retval = "image/jpeg"; break;
case ".jpg": retval = "image/jpeg"; break;
case ".jps": retval = "image/x-jps"; break;
case ".js": retval = "application/x-javascript"; break;
case ".jut": retval = "image/jutvision"; break;
case ".kar": retval = "audio/midi"; break;
case ".ksh": retval = "application/x-ksh"; break;
case ".la": retval = "audio/nspaudio"; break;
case ".lam": retval = "audio/x-liveaudio"; break;
case ".latex": retval = "application/x-latex"; break;
case ".lha": retval = "application/octet-stream"; break;
case ".lhx": retval = "application/octet-stream"; break;
case ".list": retval = "text/plain"; break;
case ".lma": retval = "audio/nspaudio"; break;
case ".log": retval = "text/plain"; break;
case ".lsp": retval = "application/x-lisp"; break;
case ".lst": retval = "text/plain"; break;
case ".lsx": retval = "text/x-la-asf"; break;
case ".ltx": retval = "application/x-latex"; break;
case ".lzh": retval = "application/octet-stream"; break;
case ".lzx": retval = "application/octet-stream"; break;
case ".m": retval = "text/plain"; break;
case ".m1v": retval = "video/mpeg"; break;
case ".m2a": retval = "audio/mpeg"; break;
case ".m2v": retval = "video/mpeg"; break;
case ".m3u": retval = "audio/x-mpequrl"; break;
case ".man": retval = "application/x-troff-man"; break;
case ".map": retval = "application/x-navimap"; break;
case ".mar": retval = "text/plain"; break;
case ".mbd": retval = "application/mbedlet"; break;
case ".mc$": retval = "application/x-magic-cap-package-1.0"; break;
case ".mcd": retval = "application/mcad"; break;
case ".mcf": retval = "text/mcf"; break;
case ".mcp": retval = "application/netmc"; break;
case ".me": retval = "application/x-troff-me"; break;
case ".mht": retval = "message/rfc822"; break;
case ".mhtml": retval = "message/rfc822"; break;
case ".mid": retval = "audio/midi"; break;
case ".midi": retval = "audio/midi"; break;
case ".mif": retval = "application/x-mif"; break;
case ".mime": retval = "message/rfc822"; break;
case ".mjf": retval = "audio/x-vnd.audioexplosion.mjuicemediafile"; break;
case ".mjpg": retval = "video/x-motion-jpeg"; break;
case ".mm": retval = "application/base64"; break;
case ".mme": retval = "application/base64"; break;
case ".mod": retval = "audio/mod"; break;
case ".moov": retval = "video/quicktime"; break;
case ".mov": retval = "video/quicktime"; break;
case ".movie": retval = "video/x-sgi-movie"; break;
case ".mp2": retval = "audio/mpeg"; break;
case ".mp3": retval = "audio/mpeg"; break;
case ".mpa": retval = "audio/mpeg"; break;
case ".mpc": retval = "application/x-project"; break;
case ".mpe": retval = "video/mpeg"; break;
case ".mpeg": retval = "video/mpeg"; break;
case ".mpg": retval = "video/mpeg"; break;
case ".mpga": retval = "audio/mpeg"; break;
case ".mpp": retval = "application/vnd.ms-project"; break;
case ".mpt": retval = "application/vnd.ms-project"; break;
case ".mpv": retval = "application/vnd.ms-project"; break;
case ".mpx": retval = "application/vnd.ms-project"; break;
case ".mrc": retval = "application/marc"; break;
case ".ms": retval = "application/x-troff-ms"; break;
case ".mv": retval = "video/x-sgi-movie"; break;
case ".my": retval = "audio/make"; break;
case ".mzz": retval = "application/x-vnd.audioexplosion.mzz"; break;
case ".nap": retval = "image/naplps"; break;
case ".naplps": retval = "image/naplps"; break;
case ".nc": retval = "application/x-netcdf"; break;
case ".ncm": retval = "application/vnd.nokia.configuration-message"; break;
case ".nif": retval = "image/x-niff"; break;
case ".niff": retval = "image/x-niff"; break;
case ".nix": retval = "application/x-mix-transfer"; break;
case ".nsc": retval = "application/x-conference"; break;
case ".nvd": retval = "application/x-navidoc"; break;
case ".o": retval = "application/octet-stream"; break;
case ".oda": retval = "application/oda"; break;
case ".ogg": retval = "audio/ogg"; break;
case ".omc": retval = "application/x-omc"; break;
case ".omcd": retval = "application/x-omcdatamaker"; break;
case ".omcr": retval = "application/x-omcregerator"; break;
case ".p": retval = "text/x-pascal"; break;
case ".p10": retval = "application/pkcs10"; break;
case ".p12": retval = "application/pkcs-12"; break;
case ".p7a": retval = "application/x-pkcs7-signature"; break;
case ".p7c": retval = "application/pkcs7-mime"; break;
case ".p7m": retval = "application/pkcs7-mime"; break;
case ".p7r": retval = "application/x-pkcs7-certreqresp"; break;
case ".p7s": retval = "application/pkcs7-signature"; break;
case ".part": retval = "application/pro_eng"; break;
case ".pas": retval = "text/pascal"; break;
case ".pbm": retval = "image/x-portable-bitmap"; break;
case ".pcl": retval = "application/vnd.hp-pcl"; break;
case ".pct": retval = "image/x-pict"; break;
case ".pcx": retval = "image/x-pcx"; break;
case ".pdb": retval = "chemical/x-pdb"; break;
case ".pdf": retval = "application/pdf"; break;
case ".pfunk": retval = "audio/make"; break;
case ".pgm": retval = "image/x-portable-greymap"; break;
case ".php": retval = "text/php"; break;
case ".pic": retval = "image/pict"; break;
case ".pict": retval = "image/pict"; break;
case ".pkg": retval = "application/x-newton-compatible-pkg"; break;
case ".pko": retval = "application/vnd.ms-pki.pko"; break;
case ".pl": retval = "text/plain"; break;
case ".plx": retval = "application/x-pixclscript"; break;
case ".pm": retval = "image/x-xpixmap"; break;
case ".pm4": retval = "application/x-pagemaker"; break;
case ".pm5": retval = "application/x-pagemaker"; break;
case ".png": retval = "image/png"; break;
case ".pnm": retval = "application/x-portable-anymap"; break;
case ".pot": retval = "application/vnd.ms-powerpoint"; break;
case ".pov": retval = "model/x-pov"; break;
case ".ppa": retval = "application/vnd.ms-powerpoint"; break;
case ".ppm": retval = "image/x-portable-pixmap"; break;
case ".pps": retval = "application/vnd.ms-powerpoint"; break;
case ".ppt": retval = "application/vnd.ms-powerpoint"; break;
case ".pptx": retval = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; break;
case ".ppz": retval = "application/vnd.ms-powerpoint"; break;
case ".pre": retval = "application/x-freelance"; break;
case ".prt": retval = "application/pro_eng"; break;
case ".ps": retval = "application/postscript"; break;
case ".psd": retval = "image/vnd.adobe.photoshop"; break;
case ".pvu": retval = "paleovu/x-pv"; break;
case ".pwz": retval = "application/vnd.ms-powerpoint"; break;
case ".py": retval = "text/x-script.phyton"; break;
case ".pyc": retval = "applicaiton/x-bytecode.python"; break;
case ".qcp": retval = "audio/vnd.qcelp"; break;
case ".qd3": retval = "x-world/x-3dmf"; break;
case ".qd3d": retval = "x-world/x-3dmf"; break;
case ".qif": retval = "image/x-quicktime"; break;
case ".qt": retval = "video/quicktime"; break;
case ".qtc": retval = "video/x-qtc"; break;
case ".qti": retval = "image/x-quicktime"; break;
case ".qtif": retval = "image/x-quicktime"; break;
case ".ra": retval = "audio/x-pn-realaudio"; break;
case ".ram": retval = "audio/x-pn-realaudio"; break;
case ".ras": retval = "application/x-cmu-raster"; break;
case ".rast": retval = "image/cmu-raster"; break;
case ".rexx": retval = "text/x-script.rexx"; break;
case ".rf": retval = "image/vnd.rn-realflash"; break;
case ".rgb": retval = "image/x-rgb"; break;
case ".rm": retval = "application/vnd.rn-realmedia"; break;
case ".rmi": retval = "audio/mid"; break;
case ".rmm": retval = "audio/x-pn-realaudio"; break;
case ".rmp": retval = "audio/x-pn-realaudio"; break;
case ".rng": retval = "application/ringing-tones"; break;
case ".rnx": retval = "application/vnd.rn-realplayer"; break;
case ".roff": retval = "application/x-troff"; break;
case ".rp": retval = "image/vnd.rn-realpix"; break;
case ".rpm": retval = "audio/x-pn-realaudio-plugin"; break;
case ".rt": retval = "text/richtext"; break;
case ".rtf": retval = "text/richtext"; break;
case ".rtx": retval = "text/richtext"; break;
case ".rv": retval = "video/vnd.rn-realvideo"; break;
case ".s": retval = "text/x-asm"; break;
case ".s3m": retval = "audio/s3m"; break;
case ".saveme": retval = "application/octet-stream"; break;
case ".sbk": retval = "application/x-tbook"; break;
case ".scm": retval = "application/x-lotusscreencam"; break;
case ".sdml": retval = "text/plain"; break;
case ".sdp": retval = "application/sdp"; break;
case ".sdr": retval = "application/sounder"; break;
case ".sea": retval = "application/sea"; break;
case ".set": retval = "application/set"; break;
case ".sgm": retval = "text/sgml"; break;
case ".sgml": retval = "text/sgml"; break;
case ".sh": retval = "application/x-sh"; break;
case ".shar": retval = "application/x-shar"; break;
case ".shtml": retval = "text/html"; break;
case ".sid": retval = "audio/x-psid"; break;
case ".sit": retval = "application/x-sit"; break;
case ".skd": retval = "application/x-koan"; break;
case ".skm": retval = "application/x-koan"; break;
case ".skp": retval = "application/x-koan"; break;
case ".skt": retval = "application/x-koan"; break;
case ".sl": retval = "application/x-seelogo"; break;
case ".smi": retval = "application/smil"; break;
case ".smil": retval = "application/smil"; break;
case ".snd": retval = "audio/basic"; break;
case ".sol": retval = "application/solids"; break;
case ".spc": retval = "text/x-speech"; break;
case ".spl": retval = "application/futuresplash"; break;
case ".spr": retval = "application/x-sprite"; break;
case ".sprite": retval = "application/x-sprite"; break;
case ".src": retval = "application/x-wais-source"; break;
case ".ssi": retval = "text/x-server-parsed-html"; break;
case ".ssm": retval = "application/streamingmedia"; break;
case ".sst": retval = "application/vnd.ms-pki.certstore"; break;
case ".step": retval = "application/step"; break;
case ".stl": retval = "application/sla"; break;
case ".stp": retval = "application/step"; break;
case ".sv4cpio": retval = "application/x-sv4cpio"; break;
case ".sv4crc": retval = "application/x-sv4crc"; break;
case ".svf": retval = "image/vnd.dwg"; break;
case ".svr": retval = "application/x-world"; break;
case ".swf": retval = "application/x-shockwave-flash"; break;
case ".t": retval = "application/x-troff"; break;
case ".talk": retval = "text/x-speech"; break;
case ".tar": retval = "application/x-tar"; break;
case ".tbk": retval = "application/toolbook"; break;
case ".tcl": retval = "application/x-tcl"; break;
case ".tcsh": retval = "text/x-script.tcsh"; break;
case ".tex": retval = "application/x-tex"; break;
case ".texi": retval = "application/x-texinfo"; break;
case ".texinfo": retval = "application/x-texinfo"; break;
case ".text": retval = "text/plain"; break;
case ".tgz": retval = "application/x-compressed"; break;
case ".tif": retval = "image/tiff"; break;
case ".tiff": retval = "image/tiff"; break;
case ".tr": retval = "application/x-troff"; break;
case ".tsi": retval = "audio/tsp-audio"; break;
case ".tsp": retval = "application/dsptype"; break;
case ".tsv": retval = "text/tab-separated-values"; break;
case ".turbot": retval = "image/florian"; break;
case ".txt": retval = "text/plain"; break;
case ".uil": retval = "text/x-uil"; break;
case ".uni": retval = "text/uri-list"; break;
case ".unis": retval = "text/uri-list"; break;
case ".unv": retval = "application/i-deas"; break;
case ".uri": retval = "text/uri-list"; break;
case ".uris": retval = "text/uri-list"; break;
case ".url": retval = "text/url"; break;
case ".ustar": retval = "application/x-ustar"; break;
case ".uu": retval = "application/octet-stream"; break;
case ".uue": retval = "text/x-uuencode"; break;
case ".vcd": retval = "application/x-cdlink"; break;
case ".vcs": retval = "text/x-vcalendar"; break;
case ".vda": retval = "application/vda"; break;
case ".vdo": retval = "video/vdo"; break;
case ".vew": retval = "application/groupwise"; break;
case ".viv": retval = "video/vivo"; break;
case ".vivo": retval = "video/vivo"; break;
case ".vmd": retval = "application/vocaltec-media-desc"; break;
case ".vmf": retval = "application/vocaltec-media-file"; break;
case ".voc": retval = "audio/voc"; break;
case ".vos": retval = "video/vosaic"; break;
case ".vox": retval = "audio/voxware"; break;
case ".vqe": retval = "audio/x-twinvq-plugin"; break;
case ".vqf": retval = "audio/x-twinvq"; break;
case ".vql": retval = "audio/x-twinvq-plugin"; break;
case ".vrml": retval = "application/x-vrml"; break;
case ".vrt": retval = "x-world/x-vrt"; break;
case ".vsd": retval = "application/x-visio"; break;
case ".vst": retval = "application/x-visio"; break;
case ".vsw": retval = "application/x-visio"; break;
case ".w60": retval = "application/wordperfect6.0"; break;
case ".w61": retval = "application/wordperfect6.1"; break;
case ".w6w": retval = "application/msword"; break;
case ".wav": retval = "audio/wav"; break;
case ".wb1": retval = "application/x-qpro"; break;
case ".wbmp": retval = "image/vnd.wap.wbmp"; break;
case ".web": retval = "application/vnd.xara"; break;
case ".wiz": retval = "application/msword"; break;
case ".wk1": retval = "application/x-123"; break;
case ".wmf": retval = "windows/metafile"; break;
case ".wml": retval = "text/vnd.wap.wml"; break;
case ".wmlc": retval = "application/vnd.wap.wmlc"; break;
case ".wmls": retval = "text/vnd.wap.wmlscript"; break;
case ".wmlsc": retval = "application/vnd.wap.wmlscriptc"; break;
case ".word": retval = "application/msword"; break;
case ".wmv": retval = "video/x-ms-wmv"; break;
case ".wp": retval = "application/wordperfect"; break;
case ".wp5": retval = "application/wordperfect"; break;
case ".wp6": retval = "application/wordperfect"; break;
case ".wpd": retval = "application/wordperfect"; break;
case ".wq1": retval = "application/x-lotus"; break;
case ".wri": retval = "application/mswrite"; break;
case ".wrl": retval = "application/x-world"; break;
case ".wrz": retval = "x-world/x-vrml"; break;
case ".wsc": retval = "text/scriplet"; break;
case ".wsrc": retval = "application/x-wais-source"; break;
case ".wtk": retval = "application/x-wintalk"; break;
case ".xbm": retval = "image/x-xbitmap"; break;
case ".xdr": retval = "video/x-amt-demorun"; break;
case ".xgz": retval = "xgl/drawing"; break;
case ".xif": retval = "image/vnd.xiff"; break;
case ".xl": retval = "application/excel"; break;
case ".xla": retval = "application/vnd.ms-excel"; break;
case ".xlb": retval = "application/vnd.ms-excel"; break;
case ".xlc": retval = "application/vnd.ms-excel"; break;
case ".xld": retval = "application/vnd.ms-excel"; break;
case ".xlk": retval = "application/vnd.ms-excel"; break;
case ".xll": retval = "application/vnd.ms-excel"; break;
case ".xlm": retval = "application/vnd.ms-excel"; break;
case ".xls": retval = "application/vnd.ms-excel"; break;
case ".xlsx": retval = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
case ".xlt": retval = "application/vnd.ms-excel"; break;
case ".xlv": retval = "application/vnd.ms-excel"; break;
case ".xlw": retval = "application/vnd.ms-excel"; break;
case ".xm": retval = "audio/xm"; break;
case ".xml": retval = "application/xml"; break;
case ".xmz": retval = "xgl/movie"; break;
case ".xpix": retval = "application/x-vnd.ls-xpix"; break;
case ".xpm": retval = "image/xpm"; break;
case ".x-png": retval = "image/png"; break;
case ".xsr": retval = "video/x-amt-showrun"; break;
case ".xwd": retval = "image/x-xwd"; break;
case ".xyz": retval = "chemical/x-pdb"; break;
case ".z": retval = "application/x-compressed"; break;
case ".zip": retval = "application/zip"; break;
case ".zoo": retval = "application/octet-stream"; break;
case ".zsh": retval = "text/x-script.zsh"; break;
default: retval = "application/octet-stream"; break;
}

return retval;
}

Mono Winforms on OS X

Despite some issues, my experience porting .NET C#/Winforms 2.0 code to a Linux system running Mono was surprisingly pleasant. Unfortunately, the same cannot be said for porting to OS X. Put simply, Mono Winforms on OS X sucks… it sucks bad… to the point where I question why it was even released.

fragmentsync mono winforms issues

The biggest and most apparent issues seems to be visibility and z-order issue. The image above is a form containing a few panels, however, only the panel containing the green header and the “sync with” label should the displayed. The others should be invisible (Control.Visible = false). Furthermore, making one control visible and another invisible or bringing a control to the front (Control.BringToFront()) such that it obscures others is a crapshoot; sometimes it’ll work correctly, other times you’ll see the correct control briefly before it disappears and your staring at a control that should be invisible or obscured.

Performance is atrocious. Winforms itself, even under .NET on Windows, is not a terribly high-performance windowing system, but it’s unbearable under OS X, to the point where you can see very prominent visual delays as events are processed and/or controls are being rendered.

Stability is awful. It’s not uncommon to reach a point where the application’s UI simply freezes up and no longer responds to events. Worse yet, disposal of Controls don’t seem to occur properly. I’ve noticed this on dynamically created controls, on a few occasions; clicking a control resulted in a crash because the control had already been disposed, but the control was still, incorrectly, being rendered in the UI and responding to events (hence the crash).

All of these problems seem to point to an issue with Mono’s per-windowing system driver for OS X. The Mono Project Roadmap indicates an upcoming Winforms update for OS X this September with the Mono 2.6 release, hopefully, it’ll address these (and I’m sure other) critical issues, so that Winforms can become truly cross-platform. As of right now, if your looking to use Mono Winforms as a solution for porting to OS X, you’ll likely only be able to pull off a port of the simplest of applications.

On a side note, Cocoa# looks like a pretty interesting project, but the website hasn’t been updated in months, so it’s possible the project is dead.

First thoughts on porting to Mono

I’ve known about Mono since its inception, but a few months ago I really became interested as support for C# 2.0 and WinForms 2.0 was announced. I played around with some code and was pretty impressed at how easy it was to get stuff compiled and running. This weekend I made a concerted effort to tackle something more complex, getting Fragment Sync compiled and running. I’ve read you can simply use Mono with the managed executable and not have to deal with compiling source code, but Fragment Sync does quite a bit of interop with unmanaged code so that really wasn’t an option.

I first attempted to import all the source files into MonoDevelop (commenting out win32 specific stuff), but this proved problematic when it came to resources. I tried a ton of different things, but could not get MonoDevelop to properly compile in the app’s resources. I then read you can just open the Visual Studio solution with MonoDevelop; this proved to be effortless and resolved the compilation issue with resources.

After a successful compiled, I had some issues getting a window up. There was an issue loading the application icon. This seems to be an issue with using Vista-compatible icons (where the 256×256 sized bitmap, is an embedded png). I changed the icon to null for a few forms to resolve this; which involves the following line in the form designer code:

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

The final and most serious issue I encountered was with Fragment Sync’s Rijndael encryption code. You can find the issue described in detail here. Note the issue will pop up when using CryptoStream as well, since CryptoStream simply abstracts the calls to the underlying Transform methods of ICryptoTransform. The trick here is to always check that you can reuse the transform. With Microsoft’s implementation you can, with Mono you can’t. So you have to recreate the transform object.

if (!decryptor.CanReuseTransform)
{
    decryptor = rij.CreateDecryptor();
}

With these issues resolved I could get things up and running.

fragment sync running on mono in opensuse

Obviously things don’t look pretty, but I think this is a good starting point to work from to get Linux support for Fragment Sync.

I should mention I did consider using Gtk# and redoing the Fragment Sync user interfaces, but I found Gtk’s design methodology unbearable… let’s just say I’m not a fan of packing widgets into containers.

.Net FtpWebRequest and 503 (bad sequence of commands) errors

I’ve been working on some FTP code in C# with the .Net framework’s FTP classes and frustrated that I was occasionally getting 503 (bad sequence of commands) errors. Turns out these errors will pop up if the FtpWebRequest.KeepAlive property is set to true, which it is by default. Unfortunately, this doesn’t really address why this is occurring in the first place. I’m doing a lot of multithreaded stuff, so perhaps that’s the culprit (i.e. 2+ threads using the same connection, layering their calls on top of each other). However, I’ve found some forum posts (such as this one) indicating others are having the same issue doing seemingly serial, non-asynchronous stuff.

Checking if a service is running in C#

Surprisingly easy. I’m doing some bluetooth stuff and needed a way to see if the bluetooth support service (BthServ) was running before attempting bluetooth related system queries and socket IO.

try
{
ServiceProcess.
ServiceController sc = new ServiceProcess.ServiceController("BthServ");
if (sc.Status != ServiceProcess.ServiceControllerStatus.Running)
{
// service not running
}
}
catch (Exception)
{
// service name not recognized
}