关闭

C中获得和修改环境变量(MSDN)

877人阅读 评论(0) 收藏 举报
/* GETENV.C: This program uses getenv to retrieve
 * the LIB environment variable and then uses
 * _putenv to change it to a new value.
 */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char *libvar;

   /* Get the value of the LIB environment variable. */
   libvar = getenv( "LIB" );

   if( libvar != NULL )
      printf( "Original LIB variable is: %s/n", libvar );

   /* Attempt to change path. Note that this only affects the environment
    * variable of the current process. The command processor's environment
    * is not changed.
    */
   _putenv( "LIB=c://mylib;c://yourlib" );

   /* Get new value. */
   libvar = getenv( "LIB" );

   if( libvar != NULL )
      printf( "New LIB variable is: %s/n", libvar );
}

Output

Original LIB variable is: C:/progra~1/devstu~1/vc/lib
New LIB variable is: c:/mylib;c:/yourlib
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:493738次
    • 积分:5035
    • 等级:
    • 排名:第5575名
    • 原创:54篇
    • 转载:106篇
    • 译文:0篇
    • 评论:127条
    最新评论