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

原创 2004年09月07日 10:56:00
 
Microsoft Knowledge Base Article - 827421 Content_subban.gif

"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
 

win10解决无法连接到此网络

前言: 由于听到周围朋友、同事介绍说win7、win8升级到win10后很好用,所以我头脑一发热也升了win10,谁知道升级之后问题多多,其中最难受的是连接wifi老是提示无法连接到此网络,基本上要连...
  • wwh001002003
  • wwh001002003
  • 2015年12月05日 14:50
  • 4487

windows系统下,Java访问共享文件夹的方法

http://hi.baidu.com/iduany/item/911685dc6f978911e1f46f72 如何通过windows服务访问网络资源(通过jcifs实现java访问网...
  • qq_34120041
  • qq_34120041
  • 2016年12月15日 09:22
  • 1881

windows无法完成格式化U盘的几种终极解决办法

现在购买的U盘,价格便宜,种类多,但是质量也比较差,很多U盘用一段时间就会出现打不开,格式化失败,提示如“windows无法完成格式化”之类。但是这并不就是意味着U盘就此要废弃了,试一试如下的方法救回...
  • qq_20545159
  • qq_20545159
  • 2015年01月07日 07:40
  • 1311

遇到问题----win10无线网络无法连接到此网络

遇到的情况-----win10系统无法连接到此网络原因看了很多个帖子发现可能的原因有以下几种:网络密码错误笔记本被加入了网络黑名单中win10的无线网卡驱动问题笔记本的无线网卡问题解决方法网络密码错误...
  • q383965374
  • q383965374
  • 2016年11月20日 18:52
  • 10714

C#在IIS上如何访问网络上的共享盘

欢迎到我的淘宝店充值话费,Q币,游戏币,官方合作伙伴,安全秒冲到帐. http://shop33449253.taobao.com/shop/view_shop.htm?spm=a1z...
  • ew3263
  • ew3263
  • 2014年03月12日 16:55
  • 916

【无法连接到网络的解决办法 】

Win8应用商店无法连接到网络的解决办法 在Windows8中,由于更改其他浏览器导致网络环境的改变最终导致应用商店无法连接网站的情况时有发生,具体错误提示为:你的电脑没有连接到internet或者绿...
  • wcgggesvv
  • wcgggesvv
  • 2013年05月30日 10:42
  • 425

win7无法访问服务器共享文件夹解决办法

方案1: 今天的工作本来安排的好好的,本来都已经在开始高效的做事情了,因为服务器要安装一个驱动程序,在安装好了之后结果发现自己的电脑再也没有办法连接到服务器的共享了,真是恼火。造理说我也是可什么...
  • win_in_action
  • win_in_action
  • 2014年03月20日 10:03
  • 6265

解决“无法连接到WMI提供程序,您没有权限或者该服务器无法访问。“

最近在学习PowerDesigner,在创建PDM模型的时候,遇到了一点儿小问题,本以为要重装SQLServer,百度了一下,问题就轻松解决了。问题的背景是这样的,在创建数据源的时候,总是测试连接不成...
  • u012904383
  • u012904383
  • 2015年02月25日 19:55
  • 4587

升级安卓6.0.1后wifi出现的已连接 但无法访问互联网的问题

问题描述:wifi右下角出现感叹号,并且显示已连接 但无法访问互联网 解决方法: 思路:通过新添加用户去掉感叹号。 设置-用户- 添加用户或个人资料-用户(用户拥有个人专属...
  • zhao_liwei
  • zhao_liwei
  • 2016年03月02日 13:43
  • 10347

无法格式化U盘的解决办法

在使用U盘的过程中难免会碰到各种各样的问题,特别是我们对U盘进行格式化的时候,经常会碰到“windows无法完成格式化”的提示,或者其他的失败信息,碰到格式化失败的情况是不是证明U盘已经不能使用了呢?...
  • EckelWei
  • EckelWei
  • 2015年05月28日 13:40
  • 411
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:在.net里面无法访问网络盘解决办法
举报原因:
原因补充:

(最多只允许输入30个字)