Cocoa 获得系统环境变量

本文介绍如何利用NSProcessInfo类来获取当前进程的环境变量,并提供了具体的代码示例,如获取LOGNAME环境变量的值。此外还概述了NSProcessInfo类的功能,包括返回进程名称、命令行参数等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 这个类 NSProcessInfo 可以获取环境变量。

NSDictionary *envDic;

envDic = [[NSProcessInfo processInfo] environment];
    if([[envDic allKeys] containsObject:@"LOGNAME"] ){   // 判断是否存在环境变量 LOGNAME
        [mainTextField setStringValue:[envDic valueForKey:@"LOGNAME"] ] ; // 获得环境变量 LOGNAME
        [seconTtextField setStringValue:@"How are you"];
    }

 

ADC 中的参考文档如下:

 

Overview

The NSProcessInfo class provides methods to access information about the current process. Each process has a single, shared NSProcessInfo object, known as process information agent.

 

这个处理程序的代理包含:命令行参数,系统环境变量,主机名,进程名。

The process information agent can return such information as the arguments, environment variables, host name, or process name. The processInfo class method returns the shared agent for the current process—that is, the process whose object sent the message. For example, the following line returns the NSProcessInfo object, which then provides the name of the current process:

NSString *processName = [[NSProcessInfo processInfo] processName];
The NSProcessInfo class also includes the operatingSystem method, which returns an enum constant identifying the operating system on which the process is executing.

NSProcessInfo objects attempt to interpret environment variables and command-line arguments in the user's default C string encoding if they cannot be converted to Unicode as UTF-8 strings. If neither conversion works, these values are ignored by the NSProcessInfo object.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值