Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,547 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,837 people online right now. Registration is fast and FREE... Join Now!




Map Drive

 
Reply to this topicStart new topic

Map Drive, Map two Drives om my PC

hendrikbez
19 Aug, 2008 - 11:35 PM
Post #1

New D.I.C Head
*

Joined: 19 Aug, 2008
Posts: 11

I want to map to two drives om my pc, sometimes when I restart my pc, I have to go into explorer and map them there, I have this code now, but....

I have try it but I still can not get it to work.

How do you tell it what the drive number is

how do you give it a username and password as there is two map drives.

is the code in the correct place.

how do I get it to work for each drive and what about the ip address of each drive

CODE
#Region "Import Files when pressing button"

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure
    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Long = &H1

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

        Dim nr As NETRESOURCE
        Dim strUsername As String
        Dim strPassword As String

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

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, 0)

        If result = 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub Import_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Import.Click

        Dim path1 As String = "Y:\Busres.txt"
        Dim path2 As String = "X:\Smeyl.txt"

        If File.Exists(path1) AndAlso File.Exists(path2) Then
            Cls_MessageBbox.Show("All files were importert", "", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

        End If

        If (Not File.Exists(path1)) Then
            Cls_MessageBbox.Show(path1 & " leër is nie op Busres Server nie." & vbNewLine & vbNewLine & "Maak seker Program het geloop op Server", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            My.Computer.FileSystem.MoveFile("Y:\BUSRES.TXT", "C:\Werk\Busres.txt", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
        End If

        If (Not File.Exists(path2)) Then
            Cls_MessageBbox.Show(path2 & " leër is nie op Busres Server nie." & vbNewLine & vbNewLine & "Maak seker Program het geloop op Server", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            My.Computer.FileSystem.MoveFile("X:\SMEYL.TXT", "C:\Werk\SM.txt", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
        End If

    End Sub

    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


This post has been edited by hendrikbez: 20 Aug, 2008 - 12:00 AM
User is offlineProfile CardPM
+Quote Post

hendrikbez
RE: Map Drive
20 Aug, 2008 - 08:07 PM
Post #2

New D.I.C Head
*

Joined: 19 Aug, 2008
Posts: 11

Any one !!!! Please help me with this code
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:00PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month