C++利用注册表添加桌面右键新建菜单

对于程序员来说,新建一个cpp文件是再频繁不过的事情了。

为了方便,我们习惯在桌面右键新建文件,而不是新建一个文本文档,然后修改后缀名。

百度谷歌查询了一下,终于知道如何添加注册表。

手痒,抽出时间用cpp写了一个程序,方便以后操作。

客户需求是永远无法满足的,经同学测试,陆续写了三个版本。

接下来直接贴代码~

第一个版本,只能添加c、cpp、java三种后缀。

 1 /*
 2  * Author: Haipz
 3  * School: HDU
 4  * File Name: registry1.0.cpp
 5  */
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <ctime>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <climits>
13 #include <cfloat>
14 #include <iostream>
15 #include <vector>
16 #include <stack>
17 #include <queue>
18 #include <set>
19 #include <map>
20 #include <algorithm>
21 using namespace std;
22 
23 char s[1024], buffer[128], result[1024*4];
24 
25 void work_1() {
26     system("reg add \"HKEY_CLASSES_ROOT\\.c\\ShellNew\" /v \"NullFile\" /t REG_SZ");
27 }
28 
29 void work_2() {
30     system("reg add \"HKEY_CLASSES_ROOT\\.cpp\\ShellNew\" /v \"NullFile\" /t REG_SZ");
31 }
32 
33 void work_3() {
34     system("reg add \"HKEY_CLASSES_ROOT\\.java\\ShellNew\" /v \"NullFile\" /t REG_SZ");
35 }
36 
37 int main() {
38     printf("Add registry for C, C++ and Java\n");
39     printf("Author: Haipz\nSchool: HDU\n");
40     printf("1 for C;\n2 for C++;\n3 for Java.\n");
41     printf("Example: 12 to add C and C++.\n");
42     printf("Please make choice(s): ");
43     gets(s);
44     for (int i = 0; s[i] != '\0'; ++i) {
45         printf("Working...\n");
46         if (s[i] == '1') work_1();
47         else if (s[i] == '2') work_2();
48         else if (s[i] == '3') work_3();
49         else printf("%c is a wrong enter!\n", s[i]);
50     }
51     system("pause");
52     return 0;
53 }

第二个版本,精简了代码,支持添加用户输入的后缀。

 1 /*
 2  * Author: Haipz
 3  * School: HDU
 4  * File Name: registry2.0.cpp
 5  */
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <ctime>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <climits>
13 #include <cfloat>
14 #include <iostream>
15 #include <vector>
16 #include <stack>
17 #include <queue>
18 #include <set>
19 #include <map>
20 #include <algorithm>
21 using namespace std;
22 
23 char a[1024];
24 char b[1024] = "reg add \"HKEY_CLASSES_ROOT\\.";
25 char c[1024] = "\\ShellNew\" /v \"NullFile\" /t REG_SZ";
26 
27 void work(char* a) {
28     strcat(b, a);
29     strcat(b, c);
30     system(b);
31 }
32 
33 int main() {
34     printf("Function: Add registry to build a new file simply!\n");
35     printf("Author: Haipz\nSchool: HDU\n");
36     printf("Example: Enter c to add C and enter cpp to add C++.\n");
37     printf("Your opion: ");
38     gets(a);
39     work(a);
40     system("pause");
41     return 0;
42 }

第三个版本,支持多次添加,并允许删除已添加的注册表。

 1 /*
 2  * Author: Haipz
 3  * School: HDU
 4  * File Name: registry2.0.cpp
 5  */
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <ctime>
 9 #include <cctype>
10 #include <cstring>
11 #include <cstdlib>
12 #include <climits>
13 #include <cfloat>
14 #include <iostream>
15 #include <vector>
16 #include <stack>
17 #include <queue>
18 #include <set>
19 #include <map>
20 #include <algorithm>
21 using namespace std;
22 
23 char key[1024];
24 char a[1024];
25 
26 void add(char* t) {
27     char b[1024] = "reg add \"HKEY_CLASSES_ROOT\\.";
28     char c[1024] = "\\ShellNew\" /v \"NullFile\" /t REG_SZ";
29     strcat(b, t);
30     strcat(b, c);
31     system(b);
32 }
33 
34 void del(char* t) {
35     char d[1024] = "reg delete \"HKEY_CLASSES_ROOT\\.";
36     char e[1024] = "\\ShellNew\" /f";
37     strcat(d, t);
38     strcat(d, e);
39     system(d);
40 }
41 
42 int main() {
43     printf("Function: Build a new file simply!\n");
44     printf("Author: Haipz\nSchool: HDU\n");
45     printf("Example: Enter \"c\" to add C and enter \"cpp\" to add C++;\n");
46     printf("         Enter \"-c\" to delete C.\n");
47     do {
48         printf("Your opion: ");
49         gets(a);
50         if (a[0] == '-') del(a + 1);
51         else add(a);
52         printf("Enter \"r\" to run again or any other key to quit: ");
53         gets(key);
54     } while (key[0] == 'r');
55     return 0;
56 }

打包下载地址:

 http://files.cnblogs.com/haipzm/Regedity.zip

注意,如果系统提示缺少某dll文件,请到网上下载,并复制到C:\Windows\System32目录下。

转载于:https://www.cnblogs.com/haipzm/p/3561468.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值