在.net里面无法访问网络盘解决办法

<script language=JavaScript src="/common/script/gsfx/toolbar.js"></script> <script language=javascript src="/library/toolbar/EN-US/global.js"></script> <script language=javascript> </script> <script type=text/javascript> var ToolbarMenu = StartMenu;</script>
 
Microsoft Knowledge Base Article - 827421

"Could not find a part of the path" error message when you access a mapped drive by using the Directory class in the Microsoft .NET Framework

View products that this article applies to.

SYMPTOMS

When you use a service that runs under a LocalSystem account or under a local user account, and that service uses the Directory class to access a mapped drive, you may receive the following error message:

Could not find a part of the path

CAUSE

A service that runs under a LocalSystem account or under a local user account can only access mapped drives that the service creates. Mapped drives are stored for each logon session. If a service runs under a LocalSystem account or under a local user account that does not create certain mapped drives, the service cannot access these mapped drives. Additionally, a service that runs under a local user account that creates certain mapped drives also receives a new set of mapped drives if you log off and then you log on again as the same local user.

WORKAROUND

To work around this problem, follow these steps:
  1. Start Microsoft Visual Studio .NET.
  2. Use either Microsoft Visual C# .NET or Microsoft Visual Basic .NET to create a Windows Service project that is named Service1.

    By default, the Service1.cs file is created in Visual C# .NET, and the Service1.vb file is created in Visual Basic .NET.
  3. If you are using Visual C# .NET, add an EventLog component to the Service1.cs form from the toolbox. If you are using Visual Basic .NET, add an EventLog component to the Service1.vb form from the toolbox.
  4. If you are using Visual C# .NET, replace the existing code in the Service1.cs file with the following code:
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Threading;
    
    namespace Service1
    {
       public class Service1 : System.ServiceProcess.ServiceBase
       {
          Thread NewThread = new Thread(new ThreadStart(Searchmapdrive.main));
          public System.Diagnostics.EventLog eventLog1;
          /// <summary> 
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components = null;
    
          public Service1()
          {
             // This call is required by the Windows.Forms Component Designer.
             InitializeComponent();
    
             // TODO: Add any initialization after the InitComponent call
          }
    
          // The main entry point for the process
          static void Main()
          {
             System.ServiceProcess.ServiceBase[] ServicesToRun;
    	
             // More than one user Service may run within the same process. To add
             // another service to this process, change the following line to
             // create a second service object.
             ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
    
             System.ServiceProcess.ServiceBase.Run(ServicesToRun);
          }
    
          /// <summary> 
          /// Required method for Designer support - do not modify 
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
             this.eventLog1 = new System.Diagnostics.EventLog();
             ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
             // 
             // Service1
             // 
             this.ServiceName = "Searchmapdrive";
             ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
    
          }
    
          /// <summary>
          /// Clean up any resources being used.
          /// </summary>
          protected override void Dispose( bool disposing )
          {
             if( disposing )
             {
                if (components != null) 
                {
                   components.Dispose();
                }
             }
             base.Dispose( disposing );
          }
    
          /// <summary>
          /// Set things in motion so your service can do its work.
          /// </summary>
          protected override void OnStart(string[] args)
          {
             // TODO: Add code here to start your service.
             NewThread.Start();
          }
     
          /// <summary>
          /// Stop this service.
          /// </summary>
          protected override void OnStop()
          {
             // TODO: Add code here to perform any tear-down necessary to stop your service.
             NewThread.Abort();
          }
       }
    }
    If you are using Visual Basic .NET, replace the existing code in the Service1.vb file with the following code:
    Imports System.ServiceProcess
    Imports System.Threading
    
    Public Class Service1
       Inherits System.ServiceProcess.ServiceBase
       Dim NewThreadStart As New ThreadStart(AddressOf Searchmapdrive.main)
       Dim NewThread As New Thread(NewThreadStart)
    
    #Region " Component Designer generated code "
    
       Public Sub New()
          MyBase.New()
    
          ' This call is required by the Component Designer.
          InitializeComponent()
    
          ' Add any initialization after the InitializeComponent() call
    
       End Sub
    
       'UserService overrides dispose to clean up the component list.
       Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
          If disposing Then
             If Not (components Is Nothing) Then
                components.Dispose()
             End If
          End If
          MyBase.Dispose(disposing)
       End Sub
    
       ' The main entry point for the process
       <MTAThread()> _
       Shared Sub Main()
          Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    
          ' More than one NT Service may run within the same process. To add
          ' another service to this process, change the following line to
          ' create a second service object.
          ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1}
    
          System.ServiceProcess.ServiceBase.Run(ServicesToRun)
       End Sub
    
       'Required by the Component Designer
       Private components As System.ComponentModel.IContainer
    
       ' NOTE: The following procedure is required by the Component Designer
       ' It can be modified using the Component Designer.  
       ' Do not modify it using the code editor.
       Friend WithEvents EventLog1 As System.Diagnostics.EventLog
       <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
          Me.EventLog1 = New System.Diagnostics.EventLog
          CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit()
          '
          'Service1
          '
          Me.ServiceName = "Searchmapdrive"
          CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit()
    
       End Sub
    
    #End Region
    
       Protected Overrides Sub OnStart(ByVal args() As String)
          ' Add code here to start your service. This method should set things
          ' in motion so your service can do its work.
          NewThread.Start()
       End Sub
    
       Protected Overrides Sub OnStop()
          ' Add code here to perform any tear-down necessary to stop your service.
          NewThread.Abort()
       End Sub
    
    End Class
  5. If you are using Visual C# .NET, add an Installer class that is named ProjectInstaller.cs to the Service1 project.

    If you are using Visual Basic .NET, add an Installer class that is named ProjectInstaller.vb to the Service1 project.
  6. If you are using Microsoft Visual Studio .NET 2003, on the Tools menu, click Add/Remove Toolbox Items.

    If you are using Visual Studio .NET 2002, on the Tools menu, click Customize Toolbox.
  7. On the .NET Framework Components tab, click to select the ServiceInstaller check box in the Customize Toolbox dialog box. Click to select the ServiceProcessInstaller check box.
  8. Click OK to close the Customize Toolbox dialog box.
  9. If you are using Visual C# .NET, add a ServiceInstaller component, and then add a ServiceProcessInstaller component from the toolbox to the ProjectInstaller.cs form.

    If you are using Visual Basic .NET, add a ServiceInstaller component, and then add a ServiceProcessInstaller component from the toolbox to the ProjectInstaller.vb form.
  10. If you are using Visual C# .NET, replace the existing code in the ProjectInstaller.cs file with the following code:
    using System.ComponentModel;
    using System.Configuration.Install;
    
    namespace Service1
    {
       /// <summary>
       /// Summary description for ProjectInstaller.
       /// </summary>
       [RunInstaller(true)]
       public class ProjectInstaller : System.Configuration.Install.Installer
       {
          private System.ServiceProcess.ServiceInstaller serviceInstaller1;
          private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components = null;
    
          public ProjectInstaller()
          {
             // This call is required by the Designer.
             InitializeComponent();
    
             // TODO: Add any initialization after the InitializeComponent call
          }
    
          /// <summary> 
          /// Clean up any resources being used.
          /// </summary>
          protected override void Dispose( bool disposing )
          {
             if( disposing )
             {
                if(components != null)
                {
                   components.Dispose();
                }
             }
             base.Dispose( disposing );
          }
    
    
          #region Component Designer generated code
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
             this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
             this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
             // 
             // serviceProcessInstaller1
             // 
             this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
             this.serviceProcessInstaller1.Password = "";
             this.serviceProcessInstaller1.Username = "";
          
             this.serviceInstaller1.ServiceName = "Searchmapdrive";
             // 
             // ProjectInstaller
             // 
             this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                                                                                      this.serviceInstaller1,
                                                                                      this.serviceProcessInstaller1});
    
          }
          #endregion
       }
    }
    If you are using Visual Basic .NET, replace the existing code in the ProjectInstaller.vb file with the following code:
    Imports System.ComponentModel
    Imports System.Configuration.Install
    
    <RunInstaller(True)> Public Class ProjectInstaller
       Inherits System.Configuration.Install.Installer
    
    #Region " Component Designer generated code "
    
       Public Sub New()
          MyBase.New()
    
          'This call is required by the Component Designer.
          InitializeComponent()
    
          'Add any initialization after the InitializeComponent() call
    
       End Sub
    
       'Installer overrides dispose to clean up the component list.
       Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
          If disposing Then
             If Not (components Is Nothing) Then
                components.Dispose()
             End If
          End If
          MyBase.Dispose(disposing)
       End Sub
    
       'Required by the Component Designer
       Private components As System.ComponentModel.IContainer
    
       'NOTE: The following procedure is required by the Component Designer
       'It can be modified using the Component Designer.  
       'Do not modify it using the code editor.
       Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
       Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
       <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
          Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller
          Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
    
          '
          'ServiceProcessInstaller1
          '
          Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User
          Me.ServiceProcessInstaller1.Password = ""
          Me.ServiceProcessInstaller1.Username = ""
          '
          'ServiceInstaller1
          '
          Me.ServiceInstaller1.ServiceName = "Searchmapdrive"
          '
          'ProjectInstaller
          '
          Me.Installers.AddRange(New System.Configuration.Install.Installer() { _
                                                                                  Me.ServiceProcessInstaller1, _
                                                                                  Me.ServiceInstaller1})
    
       End Sub
    
    #End Region
    
    End Class
  11. If you are using Visual C# .NET, add a new class that is named Searchmapdrive.cs to the Service1 project.

    If you are using Visual Basic .NET, add a new module that is named Searchmapdrive.vb to the Service1 project.
  12. If you are using Visual C# .NET, replace the existing code in the Searchmapdrive.cs file with the following code.

    Note You must assign a valid Universal Naming Convention (UNC) path of the strDirectory variable in the following code:
    using System.IO;
    using System.Diagnostics;
    
    namespace Service1
    {
       /// <summary>
       /// Summary description for Searchmapdrive.
       /// </summary>
       public class Searchmapdrive
       {
          public Searchmapdrive()
          {
             //
             // TODO: Add constructor logic here
             //
          }
    
          public static void main()
          {
             string strDirectory;
             // Assign a UNC path.
             strDirectory = "Computername//Sharename"; 
             DirSearch(strDirectory);
             Service1 searchServiceSuccess = new Service1();
             searchServiceSuccess.eventLog1.Source = "Searchmapdrive";
             searchServiceSuccess.eventLog1.WriteEntry("SearchMapDrive returned ", EventLogEntryType.Information);
          }
    
          public static void DirSearch(string sDir)
          {
             try
             { 
                foreach (string strDir in Directory.GetDirectories(sDir)) 
                {
                   foreach (string strFile in Directory.GetFiles(sDir, "*.*"))
                   {
                      // Process the files.
                   }
                   DirSearch(strDir);
                }
             }
                // Catch any exceptions and write these exceptions to the event log.
             catch(System.Exception excpt)
             {
                Service1 searchServiceError = new Service1();
                searchServiceError.eventLog1.Source = "Searchmapdrive";
                searchServiceError.eventLog1.WriteEntry("Logged Errors " + excpt.Message,EventLogEntryType.Error);
             }
          }
       }
    }
    If you are using Visual Basic .NET, replace the existing code in the Searchmapdrive.vb file with the following code.

    Note You must assign a valid UNC path of the strDirectory variable in the following code:
    Imports System.IO
    
    Module Searchmapdrive
       Public searchService As New Service1
       Public MyValue As Integer
       Public file As String
       Sub main()
          Dim strDirectory As String
          ' Assign a UNC path.
          strDirectory = "//Computername/Sharename"
          DirSearch(strDirectory)
          searchService.EventLog1.WriteEntry("SearchMapDrive", _
                                        "SearchMapDrive returned " & file, EventLogEntryType.Information)
       End Sub
       Sub DirSearch(ByVal sDir As String)
          Dim d As String
          Dim f As String
    
          Try
             For Each d In Directory.GetDirectories(sDir)
                For Each f In Directory.GetFiles(sDir, "*.*")
                   ' Process the files.
                Next
                DirSearch(d)
             Next
             ' Catch any exceptions and write these exceptions to the event log. 
          Catch excpt As System.Exception
             searchService.EventLog1.WriteEntry("SearchMapDrive", _
                                        "Logged Errors " & Err.Number & " " & Err.Description, EventLogEntryType.Error)
          End Try
       End Sub
    
    End Module
  13. Create a local user account that has user name credentials and password credentials that you can use to access the UNC path that you assigned in the previous step.

    Note Make sure that this local user account has administrative credentials.
  14. On the Build menu, click Build Solution to build the Service1.exe file.
  15. Start a Visual Studio .NET command prompt. Move to the folder where you created the Service1.exe file in the previous step.
  16. Run the following command at the Visual Studio .NET command prompt to install the Service1 application as a service:
    InstallUtil Service1.exe
  17. The Set Service Login dialog box appears. Type ComputerName/UserName in the Username text box, type Password in the Password text box, and then type Password in the Confirm password text box.

    Note The following placeholders are used in this step:
    • ComputerName is a placeholder for the name of the computer that hosts the Service1 application.
    • UserName is a placeholder for the user name that you created in step 13.
    • Password is a placeholder for the password that you created in step 13.
  18. Click OK to close the Set Service Login dialog box.
  19. Start Microsoft Windows Services Control Manager.
  20. Right-click the Searchmapdrive service, and then click Start.
  21. In Event Viewer, view the events in the application log to make sure that no errors occur.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Problem

Note If the service that you created in the "Workaround" section is already installed, you must remove that service and then follow these steps:
  1. Complete step 1 to step 10 in the "Workaround" section.
  2. If you are using Visual C# .NET, locate the following code in the ProjectInstaller.cs file:
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
    this.serviceProcessInstaller1.Password = "";
    this.serviceProcessInstaller1.Username = "";
    If you are using Visual Basic .NET, locate the following code in the ProjectInstaller.vb file:
    Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User
    Me.ServiceProcessInstaller1.Password = ""
    Me.ServiceProcessInstaller1.Username = ""
  3. If you are using Visual C# .NET, replace the code that you located in the previous step with the following code:
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;
    If you are using Visual Basic .NET, replace the code that you located in the previous step with the following code:
    Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
    Me.ServiceProcessInstaller1.Password = Nothing
    Me.ServiceProcessInstaller1.Username = Nothing
  4. Complete step 11 and step 12 in the "Workaround" section.
  5. If you are using Visual C# .NET, locate the following code in the Searchmapdrive.cs file:
    // Assign a UNC path.
    strDirectory = "Computername//Sharename"; 
    If you are using Visual Basic .NET, locate the following code in the Searchmapdrive.vb file:
    ' Assign a UNC path.
    strDirectory = "//Computername/Sharename"
  6. If you are using Visual C# .NET, replace the code that you located in the previous step with the following code.

    Note In the following code, the value of the strDirectory variable must be a hard disk drive that is mapped to a shared folder on another computer.
    // Assign a mapped drive.
    strDirectory = "Z://";
    If you are using Visual Basic .NET, replace the code that you located in the previous step with the following code.

    Note In the following code, the value of the strDirectory variable must be a hard disk drive that is mapped to a shared folder on another computer.
    ' Assign a mapped drive.
    strDirectory = "Z:/"
  7. Complete step 14 to step 16 in the "Workaround" section.
  8. Complete step 19 and step 20 in the "Workaround" section.
  9. In Event Viewer, view the most recent entries in the application log to make sure that the directory search is successful.

REFERENCES


For additional information about the Directory.GetDirectories method, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIODirectoryClassGetDirectoriesTopic1.asp

The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
Last Reviewed:1/5/2004 (1.0)
Keywords:kbServiceProcess kbService kbDirectory kbprb KB827421 kbAudDeveloper
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值