2010-12-13 61 views
0

這些代碼允許我訪問註冊表並獲取不可讀格式的最後訪問的MRU值。我在這裏做的是將它們轉換成可讀格式。我把它們放在一個數組中並輸出到控制檯中。C#註冊表(如何從數組中刪除字母之間的間距)?

輸出是這樣的:
C : \ P r o g r a m F i l e s \ i P o d \ f i l e . t x t

如何刪除的間距介於兩者之間?

在此先感謝。

try 
     { 
      RegistryKey rk = Registry.CurrentUser; 

      rk = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU", false); 
      PrintKeys(rk); 
     } 

     catch (Exception MyError) 
     { 
      Console.WriteLine("An error has occurred: " + MyError.Message); 
     } 
    } 

    static void PrintKeys(RegistryKey rk) 
    { 
     if (rk == null) 
     { 
      Console.WriteLine("No specified registry key!"); 
      return; 
     } 

     String[] names = rk.GetValueNames(); 

     Console.WriteLine("Subkeys of " + rk.Name); 
     Console.WriteLine("-----------------------------------------------"); 

     foreach (String s in names) 
     { 
      try 
      { 
       if (s == "MRUList") 
       { 
        continue; 
       } 

       else 
       { 
        try 
        { 
         Byte[] byteValue = (Byte[])rk.GetValue(s); 

         string str = BitConverter.ToString(byteValue).Replace("00", ""); 

         str = BitConverter.ToString(byteValue).Replace("-", ""); 
     //str binry AND VAR CONVERTED TEXT 

         Console.WriteLine(s + " Contains the value of : " + str); 

         StringBuilder sb = new StringBuilder(); 

         for (int i = 0; i <= str.Length - 2; i += 2) 
         { 
          sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(str.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)))); 
         } 

         String val = Convert.ToString(sb).Replace(" ", ""); 

         Console.WriteLine(s + " Contains the value of : " + val); 
        } 

        catch (Exception Errors) 
        { 
         Console.WriteLine("An error has occurred: " + Errors.Message); 
        } 
       } 

       //rk.Close(); 
      } 

      catch (Exception MyError) 
      { 
       Console.WriteLine("An error has occurred: " + MyError.Message); 
      } 

      Console.WriteLine("-----------------------------------------------"); 
      //rk.Close(); 
     } 

回答

0

該二進制碼有unicode編碼。我修復了你的代碼,並驗證它正在使用我的XP。但是,它不適用於Windows 7或Vista,因爲LastVisitedMRU不再存在。

static void Main(string[] args) 
    { 
     try 
     { 
      RegistryKey rk = Registry.CurrentUser; 
      rk = rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU", false); 
      PrintKeys(rk); 
     } 

     catch (Exception ex) 
     { 
      Console.WriteLine("An error has occurred: " + ex.Message); 
     } 
    } 

    static void PrintKeys(RegistryKey rk) 
    { 
     if (rk == null) 
     { 
      Console.WriteLine("No specified registry key!"); 
      return; 
     } 

     foreach (String s in rk.GetValueNames()) 
     { 
      if (s == "MRUList") 
      { 
       continue; 
      } 
      Byte[] byteValue = (Byte[])rk.GetValue(s); 

      UnicodeEncoding unicode = new UnicodeEncoding(); 
      string val = unicode.GetString(byteValue); 

      Console.Out.WriteLine(val); 
     } 
     Console.ReadLine(); 
    } 
+0

謝謝,忘了提及它只存在於XP中。 – athgap 2010-12-13 05:59:49