|
-
Aug 30th, 2005, 02:18 PM
#1
Thread Starter
New Member
Mapping network drive
I am trying to map a network drive, and I am trying to use the following:
VB Code:
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", "", "G:")
WNetAddConnection("\\server1\folderB", "", "H:")
End Sub
I need this to be executed when the form loads, however, when I do this under the declaration
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
I get errors for "Declare", "Private Sub", and for "WNetAddConnection". The erros are as follows:
"Declare" = Keyword is not valid as an identifier
"Private Sub" = Statement cannot appear within a method body. End of method assumed
"WNetAddConnection" = Name 'WNetAddConnection' is not declared.
I have to warn you guys, I am fairly new to VB so if I am doing something stupid, please take it easy on me. I appreciate your help.
FMNeo25
-
Aug 30th, 2005, 05:41 PM
#2
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.
-
Aug 30th, 2005, 06:02 PM
#3
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.
-
Aug 31st, 2005, 10:17 AM
#4
Thread Starter
New Member
Re: Mapping network drive
 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
-
Aug 31st, 2005, 12:44 PM
#5
Fanatic Member
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 ?
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Aug 31st, 2005, 01:03 PM
#6
Thread Starter
New Member
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
-
Aug 31st, 2005, 05:52 PM
#7
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|