118.编程实现添加环境变量
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <Windows.h> 4 5 void main() 6 { 7 //获取环境变量 8 char *path = getenv("PATH"); 9 //把获取的环境变量输出 10 printf("%s\n\n", path); 11 //要添加的路径 12 char addpath[100] = ";F:\\杂乱test\\printf可变参数\\Debug"; 13 14 //新的路径 15 char *newpath = NULL; 16 //格式化处理 17 newpath = (char *)calloc(4096, 1); 18 strcpy(newpath, "PATH="); 19 strcat(newpath, path); 20 strcat(newpath, addpath); 21 22 //设置当前程序生效(临时环境变量) 23 _putenv(newpath); 24 25 system("printf可变参数.exe"); 26 //输出所有path的分量 27 /*int i = 0; 28 while (environ[i]) 29 { 30 printf("->%s\n", environ[i]); 31 i++; 32 }*/ 33 34 system("pause"); 35 }