Sylloge
A C# helper library
code/App/Sylloge.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 namespace Sylloge
00017 {
00021     public static partial class App
00022     {
00023         private static string m_degmsgcap = string.Empty;
00024 
00042         public static bool AssociateExtensions(string ext, string exePath, string shortName, string description, bool checkExists)
00043         {
00044 
00045             if (string.IsNullOrEmpty(ext)) { throw new System.ArgumentException("Extension cannot be empth"); }
00046             if (string.IsNullOrEmpty(exePath)) { throw new System.ArgumentException("Exe path cannot be empty"); }
00047             if (string.IsNullOrEmpty(shortName)) { throw new System.ArgumentException("Short name cannot be empty"); }
00048             if (string.IsNullOrEmpty(description)) { throw new System.ArgumentException("Description cannot be empty"); }
00049             try {
00050                 string fname = shortName + ext;
00051                 string scKey = "Software\\Classes";
00052                 // Check if the keys exist and bail if we specify to
00053                 if (checkExists &&
00054                     Microsoft.Win32.Registry.CurrentUser.OpenSubKey(scKey + "\\" + ext) != null &&
00055                     Microsoft.Win32.Registry.CurrentUser.OpenSubKey(scKey + "\\" + fname) != null) {
00056                     return true; // Return if we specified to check if the keys exists AND only if they do (!null)
00057                 }
00058                 // Create the keys
00059                 Microsoft.Win32.RegistryKey sc = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(scKey);
00060                 Microsoft.Win32.RegistryKey progKey = sc.CreateSubKey(fname);
00061                 Microsoft.Win32.RegistryKey extKey = sc.CreateSubKey(ext);
00062                 if (progKey == null || extKey == null) { return false; } // There was an error opening a key so bail
00063                 extKey.SetValue("", fname); // Set the extinsion to register
00064                 progKey.SetValue("", description); // Set the description for the file
00065                 // Need to tell explorer what to do when we double click the file
00066                 Microsoft.Win32.RegistryKey cmdKey = progKey.CreateSubKey("shell\\open\\command");
00067                 if (cmdKey == null) { return false; } // Couldn't create the command sub key
00068                 cmdKey.SetValue("", ("\"" + exePath + "\" \"%1\"")); // Set the application path
00069                 // Set the icon for the files with the registered extision
00070                 Microsoft.Win32.RegistryKey ico = progKey.CreateSubKey("DefaultIcon");
00071                 if (ico == null) { return false; } // Couldn't create 'DefaultIcon' sub key
00072                 ico.SetValue("", exePath + ",0");
00073                 // Notify Windows to update its Icons
00074                 Sylloge.WinAPI.Shell32.SHChangeNotify(Sylloge.WinAPI.Constants.SHCNE_ASSOCCHANGED, Sylloge.WinAPI.Constants.SHCNF_IDLIST, 0, 0);
00075             } catch (System.Exception ex) {
00076                 Sylloge.App.Out("Could not associate extensions: " + ex.Message);
00077                 return false;
00078             }
00079             return true;
00080         }
00081 
00087         public static string GetOSName(Sylloge.WinAPI.WindowsVersion version)
00088         {
00089             switch (version) {
00090                 case Sylloge.WinAPI.WindowsVersion.Win95: return "Windows 95";
00091                 case Sylloge.WinAPI.WindowsVersion.Win98: return "Windows 98";
00092                 case Sylloge.WinAPI.WindowsVersion.WinME: return "Windows ME";
00093                 case Sylloge.WinAPI.WindowsVersion.WinNT351: return "Windows NT 3.51";
00094                 case Sylloge.WinAPI.WindowsVersion.WinNT40: return "Windows NT 4.0";
00095                 case Sylloge.WinAPI.WindowsVersion.Win2000: return "Windows 2000";
00096                 case Sylloge.WinAPI.WindowsVersion.WinServer2000: return "Windows 2000 Server";
00097                 case Sylloge.WinAPI.WindowsVersion.WinXP: return "Windows XP";
00098                 case Sylloge.WinAPI.WindowsVersion.WinServer2003: return "Windows Server 2003";
00099                 case Sylloge.WinAPI.WindowsVersion.WinVista: return "Windows Vista";
00100                 case Sylloge.WinAPI.WindowsVersion.Win7: return "Windows 7";
00101                 case Sylloge.WinAPI.WindowsVersion.WinServer2008: return "Windows Server 2008";
00102                 case Sylloge.WinAPI.WindowsVersion.Win8: return "Widnows 8";
00103             }
00104             return "Unknown";
00105         }
00106 
00111         public static Sylloge.WinAPI.WindowsVersion GetOSVersion()
00112         {
00113             // TODO: need more complete list, possible marshal down to WinAPI
00114             // call for more complete info
00115             System.PlatformID p = System.Environment.OSVersion.Platform;
00116             int maj = System.Environment.OSVersion.Version.Major;
00117             int min = System.Environment.OSVersion.Version.Minor;
00118             switch (p) {
00119                 case System.PlatformID.Win32Windows:
00120                     switch (min) {
00121                         case 0: return Sylloge.WinAPI.WindowsVersion.Win95;
00122                         case 10: return Sylloge.WinAPI.WindowsVersion.Win98;
00123                         case 90: return Sylloge.WinAPI.WindowsVersion.WinME;
00124                     }
00125                     break;
00126                 case System.PlatformID.Win32NT:
00127                     switch (maj) {
00128                         case 3: return Sylloge.WinAPI.WindowsVersion.WinNT351;
00129                         case 4: return Sylloge.WinAPI.WindowsVersion.WinNT40;
00130                         case 5:
00131                             switch (min) {
00132                                 case 0: return Sylloge.WinAPI.WindowsVersion.Win2000;
00133                                 case 1: return Sylloge.WinAPI.WindowsVersion.WinXP;
00134                                 case 2: return Sylloge.WinAPI.WindowsVersion.WinServer2003;
00135                             }
00136                             break;
00137                         case 6:
00138                             switch (min) {
00139                                 case 0: return Sylloge.WinAPI.WindowsVersion.WinVista;
00140                                 case 1: return Sylloge.WinAPI.WindowsVersion.Win7;
00141                                 case 2: return Sylloge.WinAPI.WindowsVersion.Win8;
00142                                 case 3: return Sylloge.WinAPI.WindowsVersion.WinServer2008;
00143                             }
00144                             break;
00145                     }
00146                     break;
00147             }
00148             return Sylloge.WinAPI.WindowsVersion.Unknown;
00149         }
00150 
00157         public static bool IsExtensionAssociated(string ext, string shortName)
00158         {
00159             if (string.IsNullOrEmpty(ext)) { throw new System.ArgumentException("Extension cannot be empth"); }
00160             if (string.IsNullOrEmpty(shortName)) { throw new System.ArgumentException("Short name cannot be empty"); }
00161             //if (string.IsNullOrEmpty(exePath)) { throw new System.ArgumentException("Exe path cannot be empty"); }
00162             //if (string.IsNullOrEmpty(description)) { throw new System.ArgumentException("Description cannot be empty"); }
00163             try {
00164                 return (Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Classes\\" + ext) != null &&
00165                     Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Classes\\" + (shortName + ext)) != null);
00166             } catch (System.Exception ex) {
00167                 Sylloge.App.Out("Could not check extensions: " + ex.Message);
00168             }
00169             return false;
00170         }
00171 
00177         public static System.Windows.Forms.DialogResult Message(string msg)
00178         {
00179             return Sylloge.App.Message(msg, App.m_degmsgcap, System.Windows.Forms.MessageBoxButtons.OK);
00180         }
00181 
00187         public static System.Windows.Forms.DialogResult Message(string msg, string caption)
00188         {
00189             return Sylloge.App.Message(msg, caption, System.Windows.Forms.MessageBoxButtons.OK);
00190         }
00191 
00198         public static System.Windows.Forms.DialogResult Message(string msg, string caption, System.Windows.Forms.MessageBoxButtons buttons)
00199         {
00200             return System.Windows.Forms.MessageBox.Show(msg, caption, buttons);
00201         }
00202 
00207         public static void Out(string msg)
00208         {
00209             System.Console.WriteLine(msg);
00210         }
00211 
00216         public static void Out(System.Exception ex)
00217         {
00218             if (ex == null) { ex = new System.Exception("null exception passed"); }
00219             Sylloge.App.Out("Exception:" + ex.ToString());
00220         }
00221 
00227         public static void Out(string msg, System.Exception ex)
00228         {
00229             if (msg == null) { msg = string.Empty; }
00230             if (ex == null) { ex = new System.Exception("null exception passed"); }
00231             System.Console.WriteLine((msg + System.Environment.NewLine + ex.ToString()));
00232         }
00233 
00238         public static string Path
00239         {
00240             get
00241             {
00242                 // This can get a URI file name (i.e. "file:\\C:\\X\\Y\Z.ABC") so we need to remove the 'file:\\'
00243                 string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
00244                 if (System.Uri.IsWellFormedUriString(path, System.UriKind.RelativeOrAbsolute)) {
00245                     path = (new System.Uri(path)).LocalPath;
00246                 }
00247                 return (path.StartsWith("file:\\") ? path.Substring(6) : path);
00248             }
00249         }
00250 
00256         public static System.Windows.Forms.DialogResult Question(string msg)
00257         {
00258             return Sylloge.App.Question(msg, App.m_degmsgcap, System.Windows.Forms.MessageBoxButtons.OK);
00259         }
00260 
00266         public static System.Windows.Forms.DialogResult Question(string msg, string caption)
00267         {
00268             return Sylloge.App.Question(msg, caption, System.Windows.Forms.MessageBoxButtons.OK);
00269         }
00270 
00277         public static System.Windows.Forms.DialogResult Question(string msg, string caption, System.Windows.Forms.MessageBoxButtons buttons)
00278         {
00279             return System.Windows.Forms.MessageBox.Show(msg, caption, buttons, System.Windows.Forms.MessageBoxIcon.Question);
00280         }
00281 
00289         public static bool RemoveExtensions(string ext, string shortName)
00290         {
00291             if (string.IsNullOrEmpty(ext)) { throw new System.ArgumentException("Extension cannot be empth"); }
00292             if (string.IsNullOrEmpty(shortName)) { throw new System.ArgumentException("Short name cannot be empty"); }
00293             try {
00294                 string fname = shortName + ext;
00295                 string scKey = "Software\\Classes";
00296                 // We don't throw an error within the 'delete' function becuase if the
00297                 // key doesn't exist, this function wouldn't do anything anyways
00298                 Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(scKey + "\\" + fname, false);
00299                 Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(scKey + "\\" + ext, false);
00300                 // Notify Windows to update its Icons
00301                 Sylloge.WinAPI.Shell32.SHChangeNotify(Sylloge.WinAPI.Constants.SHCNE_ASSOCCHANGED, Sylloge.WinAPI.Constants.SHCNF_IDLIST, 0, 0);
00302             } catch (System.Exception ex) {
00303                 Sylloge.App.Out(ex);
00304                 return false;
00305             }
00306             return true;
00307         }
00308 
00314         public static void SetDefaultMessageCaption(string cap)
00315         {
00316             App.m_degmsgcap = cap;
00317         }
00318     }
00319 
00320 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events