Microsoft KB Archive/827421

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

Article ID: 827421

Article Last Modified on 4/19/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





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:  Start Microsoft Visual Studio .NET. 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. 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.  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; ///      /// Required designer variable. ///      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); }

///      /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///      private void InitializeComponent {        this.eventLog1 = new System.Diagnostics.EventLog; ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit; //         // Service1 //         this.ServiceName = &quot;Searchmapdrive&quot;; ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit;

}

///      /// Clean up any resources being used. ///      protected override void Dispose( bool disposing ) {        if( disposing ) {           if (components != null) {              components.Dispose; }        }         base.Dispose( disposing ); }

///      /// Set things in motion so your service can do its work. ///      protected override void OnStart(string[] args) {        // TODO: Add code here to start your service. NewThread.Start; }     ///       /// Stop this service. ///      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)


 * 1) Region &quot; Component Designer generated code &quot;

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  _ 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  Private Sub InitializeComponent Me.EventLog1 = New System.Diagnostics.EventLog CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit '     'Service1 '     Me.ServiceName = &quot;Searchmapdrive&quot; CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit

End Sub


 * 1) 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  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.</li> 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.</li> 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.</li> Click OK to close the Customize Toolbox dialog box.</li> 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.</li>  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 description for ProjectInstaller. ///   [RunInstaller(true)] public class ProjectInstaller : System.Configuration.Install.Installer {     private System.ServiceProcess.ServiceInstaller serviceInstaller1; private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; ///      /// Required designer variable. ///      private System.ComponentModel.Container components = null;

public ProjectInstaller {        // This call is required by the Designer. InitializeComponent;

// TODO: Add any initialization after the InitializeComponent call }

///      /// Clean up any resources being used. ///      protected override void Dispose( bool disposing ) {        if( disposing ) {           if(components != null) {              components.Dispose; }        }         base.Dispose( disposing ); }

#region Component Designer generated code ///      /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///      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 = &quot;&quot;; this.serviceProcessInstaller1.Username = &quot;&quot;; this.serviceInstaller1.ServiceName = &quot;Searchmapdrive&quot;; //         // 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


 * 1) Region &quot; Component Designer generated code &quot;

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 = &quot;&quot; Me.ServiceProcessInstaller1.Username = &quot;&quot; '     'ServiceInstaller1 '     Me.ServiceInstaller1.ServiceName = &quot;Searchmapdrive&quot; '     'ProjectInstaller '     Me.Installers.AddRange(New System.Configuration.Install.Installer { _                                                                              Me.ServiceProcessInstaller1, _                                                                              Me.ServiceInstaller1})

End Sub


 * 1) End Region

End Class </li> 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.</li>  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 description for Searchmapdrive. ///   public class Searchmapdrive {     public Searchmapdrive {        //         // TODO: Add constructor logic here //     }

public static void main {        string strDirectory; // Assign a UNC path. strDirectory = &quot;\\\\Computername\\Sharename&quot;; DirSearch(strDirectory); Service1 searchServiceSuccess = new Service1; searchServiceSuccess.eventLog1.Source = &quot;Searchmapdrive&quot;; searchServiceSuccess.eventLog1.WriteEntry(&quot;SearchMapDrive returned &quot;, EventLogEntryType.Information); }

public static void DirSearch(string sDir) {        try {            foreach (string strDir in Directory.GetDirectories(sDir)) {              foreach (string strFile in Directory.GetFiles(sDir, &quot;*.*&quot;)) {                 // 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 = &quot;Searchmapdrive&quot;; searchServiceError.eventLog1.WriteEntry(&quot;Logged Errors &quot; + 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 = &quot;\\Computername\Sharename&quot; DirSearch(strDirectory) searchService.EventLog1.WriteEntry(&quot;SearchMapDrive&quot;, _                                   &quot;SearchMapDrive returned &quot; & 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, &quot;*.*&quot;) ' 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(&quot;SearchMapDrive&quot;, _                                   &quot;Logged Errors &quot; & Err.Number & &quot; &quot; & Err.Description, EventLogEntryType.Error) End Try End Sub

End Module </li> 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.</li> On the Build menu, click Build Solution to build the Service1.exe file.</li> Start a Visual Studio .NET command prompt. Move to the folder where you created the Service1.exe file in the previous step.</li>  Run the following command at the Visual Studio .NET command prompt to install the Service1 application as a service: InstallUtil Service1.exe </li> The Set Service Login dialog box appears. Type \ in the Username text box, type  in the Password text box, and then type  in the Confirm password text box.

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

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Problem
Note If the service that you created in the &quot;Workaround&quot; section is already installed, you must remove that service and then follow these steps: <ol> <li>Complete step 1 to step 10 in the &quot;Workaround&quot; section.</li> <li> 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 = &quot;&quot;; this.serviceProcessInstaller1.Username = &quot;&quot;; 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 = &quot;&quot; Me.ServiceProcessInstaller1.Username = &quot;&quot; </li> <li> 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 </li> <li>Complete step 11 and step 12 in the &quot;Workaround&quot; section.</li> <li> If you are using Visual C# .NET, locate the following code in the Searchmapdrive.cs file: // Assign a UNC path. strDirectory = &quot;\\\\Computername\\Sharename&quot;; If you are using Visual Basic .NET, locate the following code in the Searchmapdrive.vb file: ' Assign a UNC path. strDirectory = &quot;\\Computername\Sharename&quot; </li> <li> 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 = &quot;Z:\\&quot;; 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 = &quot;Z:\&quot; </li> <li>Complete step 14 to step 16 in the &quot;Workaround&quot; section.</li> <li>Complete step 19 and step 20 in the &quot;Workaround&quot; section.</li> <li>In Event Viewer, view the most recent entries in the application log to make sure that the directory search is successful.</li></ol>

<div class="references_section">