"网络使用与QUOT;命令在Windows服务 [英] "net use" command in a Windows Service

查看:149
本文介绍了"网络使用与QUOT;命令在Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用下面的命令行从Windows服务中与开发C#.NET Framework 1.1的:

  NET USE Z:\\ MYCOMPUTER \ C $
 

该服务是在是在我的电脑本地管理员的域帐户运行。 Z:调试code,我们可以看到,它不返回任何错误,但在后驱动器永远不会映射。我们试图从控制台应用程序完全相同的code和它工作正常。它是什么,我们需要添加的服务,使这项工作?

我们所使用的code列述于后。

问候,
塞尔吉奥·

  startInfo.FileName =净;
startInfo.Arguments =的String.Format(@使用{0}:\\ {1} \ {2},驱动器号,
                                    计算机名称,文件夹).Trim();
startInfo.UseShellExecute = FALSE;
startInfo.RedirectStandardError = TRUE;

proc.EnableRaisingEvents = FALSE;
proc.StartInfo = StartInfo的;

proc.Start();

//如果有该驱动器的映射过程中发生错误时,将读
//从StandardError的属性,它是一个StreamReader对象,
//被送入误差输出参数。
使用(StreamReader的errorReader = proc.StandardError)
{
         StandardError的字符串的String.Empty =;
    而((StandardError的= errorReader.ReadLine())!= NULL)
    {
    错误+ = StandardError的+;
    }
}
proc.WaitForExit();
 

解决方案

从<一个href="http://msdn.microsoft.com/en-us/library/ms685143.aspx">http://msdn.microsoft.com/en-us/library/ms685143.aspx:

  

一个服务(或任何进程在运行   不同的安全上下文),必须   访问远程资源应使用   通用命名约定(UNC)   名称来访问该资源。该   服务必须有适当的   特权访问资源。如果   服务器端的服务使用的RPC   连接,代表团必须启用   在远程服务器上。

     

驱动器号不是全球性的   系统。每个登录会话接收   一套自己的驱动器号,从A到   Z.因此,重定向的驱动器不能   运行进程之间共享   在不同的用户帐户。   另外,服务(或任何过程   它自己的登录会话中运行)   无法访问的驱动器号   内的不同建立了   登录会话。

     

一个服务不应该直接访问   通过本地或网络资源   映射驱动器盘符,也不应   调用net use命令映射驱动器   信在运行时。

We're using the following command line from within a Windows Service developed with C# .Net Framework 1.1:

net use z: \\myComputer\c$

The service is running under a domain account that is a local administrator on "myComputer". After debugging the code we can see that it does not return any errors but the "z:" drive is never mapped. We've tried the exact same code from a console application and it works properly. What is it that we need to add to the Service to make this work?

The code we're using is included below.

Regards,
Sergio

startInfo.FileName = "net";  
startInfo.Arguments = string.Format(@"use {0}: \\{1}\{2}", driveLetter,
                                    computerName, folder).Trim();  
startInfo.UseShellExecute = false;  
startInfo.RedirectStandardError = true;

proc.EnableRaisingEvents = false;  
proc.StartInfo = startInfo;

proc.Start();

// If there is an error during the mapping of the drive, it will be read
// from the StandardError property which is a StreamReader object and
// be fed into the error output parameter.  
using(StreamReader errorReader = proc.StandardError)  
{  
         string standardError = string.Empty;  
    while((standardError = errorReader.ReadLine()) != null)  
    {  
    	error += standardError + " ";  
    }  
}  
proc.WaitForExit();

解决方案

From http://msdn.microsoft.com/en-us/library/ms685143.aspx:

A service (or any process running in a different security context) that must access a remote resource should use the Universal Naming Convention (UNC) name to access the resource. The service must have appropriate privileges to access the resource. If a server-side service uses an RPC connection, delegation must be enabled on the remote server.

Drive letters are not global to the system. Each logon session receives its own set of drive letters from A to Z. Therefore, redirected drives cannot be shared between processes running under different user accounts. Moreover, a service (or any process running within its own logon session) cannot access the drive letters that were established within a different logon session.

A service should not directly access local or network resources through mapped drive letters, nor should it call the net use command to map drive letters at run time.

这篇关于&QUOT;网络使用与QUOT;命令在Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆