2011-04-22 63 views
2

我爲副教授*。自衛隊文件此代碼,以我的C#程序:問題,在我的程序關聯的文件類型

public class FileAssociation 
     { 
      // Associate file extension with progID, description, icon and application 
      public static void Associate(string extension, 
        string progID, string description, string icon, string application) 
      { 
       Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID); 
       if (progID != null && progID.Length > 0) 
        using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID)) 
        { 
         if (description != null) 
          key.SetValue("", description); 
         if (icon != null) 
          key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon)); 
         if (application != null) 
          key.CreateSubKey(@"Shell\Open\Command").SetValue("", 
             ToShortPathName(application) + " \"%1\""); 
        } 
      } 

      // Return true if extension already associated in registry 
      public static bool IsAssociated(string extension) 
      { 
       return (Registry.ClassesRoot.OpenSubKey(extension, false) != null); 
      } 

      [DllImport("Kernel32.dll")] 
      private static extern uint GetShortPathName(string lpszLongPath, 
       [Out] StringBuilder lpszShortPath, uint cchBuffer); 

      // Return short path format of a file name 
      private static string ToShortPathName(string longName) 
      { 
       StringBuilder s = new StringBuilder(1000); 
       uint iSize = (uint)s.Capacity; 
       uint iRet = GetShortPathName(longName, s, iSize); 
       return s.ToString(); 
      } 
     } 

我用這樣的:

if (!FileAssociation.IsAssociated(".sdf")) 
       FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe"); 

我試試這個太:

if (FileAssociation.IsAssociated(".sdf")) 
       FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe"); 

我的問題是當文件已經與另一個程序關聯它不會工作!

例如:在我的電腦協理的*.sdf文件與Visual-studio 2008

我運行這段代碼打開 - 並沒有任何反應!

我能做些什麼?

在此先感謝

回答

0

嘗試先刪除鍵,然後編寫自己的。