http://hi.baidu.com/study_cs/item/03d6ce26d8b58e4247996223
在perl中获取当前登陆的用户名,要求在windows和linux下都可用.通过读取系统的环境变量实现,在xp和linux下测试通过,代码如下:
my $Sys_name = $^O;
if ($Sys_name =~ /MSWin32/){
print $ENV{'USERNAME'},"\n";
}
else {
if($Sys_name =~ /linux/){
print $ENV{'USER'},"\n";
}
else
{
print "Unknow\n";
}
}
摘自:http://rainbird.blog.51cto.com/211214/149961
其他:http://www.cnblogs.com/royenhome/archive/2010/07/21/1782019.html
1、获取操作系统版本信息
use Win32;
use strict;
($OS_string, $OS_major, $OS_minor, $OS_build, $OS_id) = Win32::GetOSVersion();
2、获取系统目录
my $systemdir= Win32::GetFolderPath(0x0025) if(Win32::GetFolderPath(0x0025));
在xp系统下$systemdir的变量值为C:\Windows\System32
3、获取Windows目录
our $windir = Win32::GetFolderPath(0x0024) if(Win32::GetFolderPath(0x0025));
xp系统下$windir的变量值为C:\WINDOWS
4、获取系统所在的磁盘驱动号
our $rootdir= $ENV{SYSTEMDRIVE} if($ENV{SYSTEMDRIVE});
xp系统下$rootdir的变量值为C:,因为我机器操作系统装在C盘
5、获取执行程序当前目录
our $currentdir = Win32::GetCwd() if(Win32::GetCwd);
6、获取用户的document文件夹
our $alldocuments = Win32::GetFolderPath(0x002e) if(Win32::GetFolderPath(0x002e));
xp系统下, $documents的变量值为C:\Documents and Settings\All Users\Documents
7、获取临时文件夹目录
our $tempdir = $ENV{TEMP} if($ENV{TEMP});
8、获取当前用户启动文件夹目录
our $mystartup = Win32::GetFolderPath(0x0007) if(Win32::GetFolderPath(0x0007));
9、获取所用用户启动文件夹目录
our $allstartup = Win32::GetFolderPath(0x0018) if(Win32::GetFolderPath(0x0018));
10、获取Program Files文件夹目录
our $programdir = Win32::GetFolderPath(0x0026) if(Win32::GetFolderPath(0x0026));
11、获取AppData文件夹目录
our $myappdata = Win32::GetFolderPath(0x001a) if(Win32::GetFolderPath(0x001a));
12、获取Desktop文件夹目录
our $mydesktop = Win32::GetFolderPath(0x0010) if(Win32::GetFolderPath(0x0010));
13、获取当前用户Favorite文件夹目录
our $myfavorites = Win32::GetFolderPath(0x0006) if(Win32::GetFolderPath(0x0006));
14、获取所有用户的Favorite文件夹目录
our $allfavorites = Win32::GetFolderPath(0x001f) if(Win32::GetFolderPath(0x001f));
15、获取启动菜单文件夹目录
our $mystartmenu = Win32::GetFolderPath(0x000b) if(Win32::GetFolderPath(0x000b));
16、获取计算机名称
our $computername = Win32::NodeName() if(Win32::NodeName());
17.、获取当前系统登录用户名
our $username = Win32::LoginName() if(Win32::LoginName());