你的分享就是我们的动力 ---﹥

Perl 在 Windows 上添加共享目录

时间:2013-05-30 00:09来源:www.chengxuyuans.com 点击:

代码简介

暂无

Perl代码片段

#!/usr/bin/perl -w

use Win32;
use Win32::NetResource;
use Win32::WinError;    # Error code

# Set up a default host name.
$node = Win32::NodeName();

# Generate "proper" UNC name.
$default_host = "\\\\" . $node;

$host = $default_host;

$directory = "d:\\MyShare";
$name   = "MyShare";
$remark = "MyShare directory";


%share_def = (
   'maxusers'    => -1,
   'netname'     => $name,  
   'passwd'      => "",
   'path'        => $directory,   
   'permissions' => 0x01 | 0x02,  # read-write
   'remark'      => $remark,
   'type'        => 0x00,  # directory
);

$parm_error = 0;   # ignore

$status = Win32::NetResource::NetShareAdd(\%share_def,$parm_error, $host);

if ($status) {
   print "Shared $directory as $host\\$name\n";
} else {
   print_netresource_error();
}


# Note that you may see no message for an error.
sub print_netresource_error {

   my($error_code) = 0;
   my($type);
   my($description);

   Win32::NetResource::GetError( $error_code );

   if ($error_code == ERROR_EXTENDED_ERROR) {

      Win32::NetResource::WNetGetLastError($error_code,
            $description, $type);

      print "Extended $type error: $description\n";
   } else {
      print "Error $error_code\n";
      print Win32::FormatMessage( $error_code );
   }
   
}

转载注明地址:http://www.chengxuyuans.com/Perl/63111.html