Re: Mapping network drive
Why do you have to create a map, rather than using UNC names? Also, be careful when hardcoding path names. Usually I keep those in a config file.
Re: Mapping network drive
Show us the entire section of code. It sounds like you are trying to make declarations in a method that need to be made at the class level.
Also, do take heed to what wild_bill says. You cannot know that those drive letters do not already exist on the current machine without checking first, and if they do then you would need to choose a different letter.
Re: Mapping network drive
Quote:
Originally Posted by jmcilhinney
Show us the entire section of code. It sounds like you are trying to make declarations in a method that need to be made at the class level.
Also, do take heed to what wild_bill says. You cannot know that those drive letters do not already exist on the current machine without checking first, and if they do then you would need to choose a different letter.
I understand about the drive. However, the reason I am doing it like that is because the drive is assigned for our use and no one else will use it. It has been reserved. Now, granted, a user can map anything that he/she wants manually to the drive I need, so in that case what I would need is to check if the drive is mapped, give a notice that the drive needs to be unmapped, then remapped to the server that houses the engineering applications. Now, for the code, what I include here is a simple new form with nothing on it but the function to map the drive when the form loads. Here is the complete code. I cut out the code below the "Windows Form Designer generated code" for brevity, denoted by "Snipp...."
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Snipp...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Private Sub Form_Load()
WNetAddConnection("\\server1\folderA", "", "H:")
WNetAddConnection("\\server1\folderB", "", "G:")
End Sub
End Sub
End Class
Thanx for your help.
VBNeo25
Re: Mapping network drive
Don't know if its a type mistake but I think your code should look like this: i.e. the Declare should be at class level instead of inside a method.
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
#Region " Windows Form Designer generated code "
Snipp...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WNetAddConnection("\\server1\folderA", "", "H:")
WNetAddConnection("\\server1\folderB", "", "G:")
End Sub
End Class
Why do you have Form_Load within Form1_Load ?
Re: Mapping network drive
Thanx for your suggestion, I entered the code as you suggested and the errors disappeared, however, the drive is not getting mapped. I also tried to put the statement in a button, but even after clicking the button the drive doesn't show up under My Computer. Here's how I did it.
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
#Region " Windows Form Designer generated code "
Snipp.....
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WNetAddConnection("\\server1\folderA", "", "H:")
End Sub
End Class
VBNeo25
Re: Mapping network drive
WNetAddConnection is a function that returns an error code. You should test that return value to see what is happening. Here is the MSDN topic for that function and it lists the error codes. Unfortunately, like most MSDN Win32 topics, it gives the names of the constants but not the numerical values. In this situation I usually do a Google search for the constant names and have always been able to find a site somewhere that gives their numerical values.