Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:37:05+00:00 2026-05-13T21:37:05+00:00

My software handles multiple operations on files, and I have now finished writing the

  • 0

My software handles multiple operations on files, and I have now finished writing the related functions, using the System.IO classes.

I now need to add support for network drives. Using a mapping works very well (although Directory.GetFiles is a bit low, and I don’t know why), but I’d now like to be able to deal directly with paths such as \\192.168.0.10\Shared Folder\MyDrive. Is there any way to handle this type of paths other than mounting the drive to an available drive letter, using the newly generated path, and then unmounting?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:37:06+00:00Added an answer on May 13, 2026 at 9:37 pm

    You can use the UNC path (which starts with \\) directly in your paths. However, you must account for the credentials for this connection, which can be the tricky part.

    There are several approaches:

    1. If the remote system is on the same domain or there is a trust relationship between the domains, and the user your program is running as has suitable access, it will “just work”.

    2. You can shell out and execute the net use command (through the Windows net.exe program) to make a connection with a specific username and password. Be aware that connection is usable by any program running in the user’s session, not just your application. Use the /DELETE command to remove the connection when you are done. The typical syntax is: net use \\computername\sharename password /USER:domain\username.

    3. You can P/Invoke WNetAddConnection2 to accomplish the same thing as net use without shelling out to net.exe. By passing NULL as lpLocalName, no drive letter is assigned, but the username and password will apply to subsequent accesses made through the UNC path. The WNetCancelConnection2 function can be used to disconnect.

    4. You can P/Invoke LogonUser with the LOGON32_LOGON_NEW_CREDENTIALS flag followed by an impersonation to add additional remote credentials to your thread. Unlike #2 and #3, the effects on the user’s entire session will be a little more limited. (In practice, this is rarely done in favor of the well-known WNetAddConnection2 solution.)

    The following is a sample of how to call WNetAddConnection2 from VB.NET.

    Private Sub Test()
        Dim nr As New NETRESOURCE
        nr.dwType = RESOURCETYPE_DISK
        nr.lpRemoteName = "\\computer\share"
        If WNetAddConnection2(nr, "password", "user", 0) <> NO_ERROR Then
            Throw New Exception("WNetAddConnection2 failed.")
        End If
        'Code to use connection here.'
        If WNetCancelConnection2("\\computer\share", 0, True) <> NO_ERROR Then
            Throw New Exception("WNetCancelConnection2 failed.")
        End If
    End Sub
    
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure NETRESOURCE
        Public dwScope As UInteger
        Public dwType As UInteger
        Public dwDisplayType As UInteger
        Public dwUsage As UInteger
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpLocalName As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpRemoteName As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpComment As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpProvider As String
    End Structure
    
    Private Const NO_ERROR As UInteger = 0
    Private Const RESOURCETYPE_DISK As UInteger = 1
    
    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetAddConnection2(ByRef lpNetResource As NETRESOURCE, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpPassword As String, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpUserName As String, ByVal dwFlags As UInteger) As UInteger
    End Function
    
    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetCancelConnection2(<[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpName As String, ByVal dwFlags As UInteger, <MarshalAs(UnmanagedType.Bool)> ByVal fForce As Boolean) As UInteger
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.