【问题标题】:C# Registry (How to remove spacing between letters from array)?C# Registry(如何从数组中删除字母之间的间距)?
【发布时间】:2010-12-13 13:33:27
【问题描述】:

这些代码允许我访问注册表并获取不可读格式的 lastvisitedMRU 值。我在这里所做的是将它们转换为可读的格式。我将它们放在一个数组中并将它们输出到控制台。

输出如下:
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();
        }

【问题讨论】:

    标签: c# registry spacing


    【解决方案1】:

    那里的二进制文件是 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();
        }
    

    【讨论】:

    • 谢谢,忘了说它只存在于XP中。
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2020-12-26
    • 2020-02-26
    • 2011-10-20
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多