Solved

.NET Networking Mapping

Posted on 2014-03-13
4
459 Views
Last Modified: 2014-03-19
I need to be able to map some networked drives as persistent and then verify they are mapped.  I also need to change a Drive letter from D: to Y: and verify that happens.  All needs to be done with preferably with VB.NET using VS2010

Thanks
Ian
0
Comment
Question by:mcsdguyian
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 39926983
Use a batch file (use the following line to create the mapping):
net use x: \\YourPath password /USER:myAccount

Open in new window


This VB code will fire it off:
Dim process As Process = Process.Start(psi)

Open in new window

0
 

Author Comment

by:mcsdguyian
ID: 39927388
I am actually trying to get away from using the batch file.  This is what I have in place already. I have no way to verify the the mapping happens and it does not always map even though it runs through the code.

I am using the following code to map and unmap the drive:

 Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

        Dim nr As NETRESOURCE
        Dim Username As String
        Dim Password As String

        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        nr.lpLocalName = DriveLetter & ":"
        Username = Nothing '(add parameters to pass this if necessary)
        Password = Nothing '(add parameters to pass this if necessary)
        nr.dwType = RESOURCETYPE_DISK

        Dim result As Integer
        result = WNetAddConnection2(nr, Password, Username, 0)

        If result = 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer

        rc = WNetCancelConnection2(DriveLetter, 0, ForceDisconnect)

        If rc = 0 Then
            Return True
        Else
            Return False
        End If

    End Function

The problem I am having though is I need to verify the connection outside of this code. Such as D: = \\folder1\temp\ Versus \\folder2\temp

I also need to figure how to change drive letters  

Ian
0
 
LVL 15

Accepted Solution

by:
David L. Hansen earned 500 total points
ID: 39927439
You can actually add the batch file into the VS project as a resource so it's all kept together in one solution.  Barring that, I'd suggest doing the same thing within PowerShell (launched from VS) but even then you'll be using the same "Net Use" command.
0
 

Author Comment

by:mcsdguyian
ID: 39927621
How am I to verify I am connected to where I need to be?  Also how would I change a Drive letter from a batch file?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

13 Experts available now in Live!

Get 1:1 Help Now