首页 新闻 会员 周边 捐助

C# 将OCX文件写入注册表,但是ocx文件路径有空格,无法写入

0
悬赏园豆:10 [已解决问题] 解决于 2020-08-04 17:16

写入注册表

System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "Regsvr32.exe";
            process.StartInfo.Arguments = Application.StartupPath + "\\" + @"MapWinGIS\MapWinGIS.ocx";
            process.StartInfo.Verb = "RunAs";
            process.Start();
            Console.WriteLine("Registe Success");

因为路径中如果存在空格,路径会被截断。怎么解决?

还有删除注册表,返回值提示删除完成,但是注册表编辑器中还是存在这条

代码:

            Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"CLSID\{03A98C90-70FF-40C7-AD93-6BF8B41B170F}",Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);
            if (registryKey != null)
            {
                Console.WriteLine("Start UnReg");
                registryKey.DeleteSubKeyTree(@"CLSID\{03A98C90-70FF-40C7-AD93-6BF8B41B170F}");
                registryKey.Close();
            }
            Microsoft.Win32.RegistryKey registryKey1 = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"CLSID\{03A98C90-70FF-40C7-AD93-6BF8B41B170F}",true);
            if (registryKey1 != null)
            {

                registryKey.DeleteSubKeyTree(@"CLSID\{03A98C90-70FF-40C7-AD93-6BF8B41B170F}");
                registryKey1.Close();

            }
            else
            {
                Console.WriteLine("Unregister success");
            }
echo_lovely的主页 echo_lovely | 小虾三级 | 园豆:1546
提问于:2020-08-04 15:09
< >
分享
最佳答案
0

了解单双引号。

for /r %a in (*) do if exist "%a" echo %a

收获园豆:10
花飘水流兮 | 专家六级 |园豆:13615 | 2020-08-04 15:26

不是很懂

echo_lovely | 园豆:1546 (小虾三级) | 2020-08-04 15:38

能说详细点吗?

echo_lovely | 园豆:1546 (小虾三级) | 2020-08-04 15:38

因为路径不确定,所以使用Application.StartupPath获取exe文件所在目录(这个目录可能会有空格),然后加上"MapWinGIS\MapWinGIS.ocx"ocx文件的路径

echo_lovely | 园豆:1546 (小虾三级) | 2020-08-04 15:43

您好,我明白你的意思了。搞定了!

若有空格,代码如下:

System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = "Regsvr32.exe";

            //就是这句话
            process.StartInfo.Arguments ='"'+ "F:\\visual studio" + "\\" + @"MapWinGIS\MapWinGIS.ocx"+'"';


            process.StartInfo.Verb = "RunAs";
            process.Start();
            Console.WriteLine("Registe Success");
echo_lovely | 园豆:1546 (小虾三级) | 2020-08-04 17:08

就是用双引号把文件路径包裹住

echo_lovely | 园豆:1546 (小虾三级) | 2020-08-04 17:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册