Solved

How to map a network drive in vb .net

Posted on 2007-12-03
6
9,026 Views
Last Modified: 2013-11-26
I wrote the below code to map a network drive to another server:

 Try
            Dim storefile As System.IO.Directory
            Dim directory As String
            Dim files As String()
            Dim File As String

            files = storefile.GetFiles("\\" & DropDownList1.SelectedItem.Value & "\Y$\Checkpoints", "*")

            For Each File In files
                Label21.Text += (File & "<BR>")
            Next

        Catch ex As System.Exception
            Label9.Text = ex.Message
        End Try

The error message I'm getting Access to the path "\\YKCM1S1\Y$\Checkpoints" is denied. The path exist on the other server. How can I get around this? Thanks
0
Comment
Question by:redouard
6 Comments
 
LVL 12

Expert Comment

by:weareit
ID: 20397888
Is Y$ an actual share on the server?

It seems as if you are trying to map to a network drive, if so then you cannot do this.  Y$ signifies an administrative share or a hidden share of a local drive on the target server.

-saige-
0
 

Author Comment

by:redouard
ID: 20397934
Okay, now I removed the Y$ and nothing is happening?
0
 
LVL 12

Expert Comment

by:weareit
ID: 20397954
On the target server what is the share that you are attempting to attatch to?

-saige-
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 20398463
Here is a class that will let you map to a network share:
' Add a COM reference to Windows Script Host Object Model.
 

Imports IWshRuntimeLibrary

Imports System.Runtime.InteropServices
 

Public Class NetworkDriveMapper
 

  Public Shared Sub MapDrive(ByVal driveLetter As String, _

   ByVal networkPath As String, ByVal isPersistent As Boolean)
 

    ' Create a new shell object.

    Dim networkShell As New WshNetwork
 

    Try
 

      ' Disconnect the drive first, forcing a permanent change.

      DisconnectDrive(driveLetter, True, True)
 

      ' Map the drive to the path.

      networkShell.MapNetworkDrive(driveLetter, networkPath, Convert.ToBoolean(isPersistent))
 

    Finally
 

      If Not networkShell Is Nothing Then

        Marshal.ReleaseComObject(networkShell)
 

        networkShell = Nothing
 

      End If
 

    End Try
 

  End Sub
 

  Public Shared Sub DisconnectDrive(ByVal driveLetter As String, _

    ByVal willForce As Boolean, ByVal isPersistent As Boolean)
 

    ' Create a new shell object.

    Dim networkShell As New WshNetwork
 

    If IO.Directory.Exists(driveLetter) Then
 

      Try
 

        networkShell.RemoveNetworkDrive(driveLetter, Convert.ToBoolean(willForce), _

            Convert.ToBoolean(isPersistent))
 

      Finally
 

        If Not networkShell Is Nothing Then

          Marshal.ReleaseComObject(networkShell)
 

          networkShell = Nothing
 

        End If
 

      End Try
 

    End If
 

  End Sub
 

End Class

Open in new window

0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591189
Forced accept.

Computer101
EE Admin
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now