ファイルサーバー接続&ドライブ割り当て
by achi on Nov.25, 2008, under VB.NETテクニック
ファイルサーバーに接続してログインし、必要なフォルダをドライブ割り当てして作業するケースでの手法。作業後はドライブを切断する。
Imports System.Data.OleDB
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 WNetCancelConnection Lib “mpr.dll” Alias “WNetCancelConnection2A” (ByVal lpName As String, ByVal lpPlof As Integer, Optional ByVal fForce As Boolean = False) As Boolean
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
Private Const RESOURCE_CONNECTED As Integer = &H1
Private Const RESOURCETYPE_ANY As Integer = &H0
Private Const RESOURCEDISPLAYTYPE_SHARE As Integer = &H3
Private Const CONNECT_UPDATE_PROFILE As Integer = &H1
Public Function ConnectSrv(ByVal pstrRemoteName As String, ByVal pstrUID As String, ByVal pstrPWD As String) As Long
Dim typNetResource As NETRESOURCE
Dim lngRet As Long
With typNetResource
.dwScope = RESOURCE_CONNECTED
.dwType = RESOURCETYPE_ANY
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.lpLocalName = “Z:” ‘実際には空きドライブを検索するのがベター
.lpRemoteName = pstrRemoteName
End With
lngRet = WNetAddConnection2(typNetResource, pstrPWD, pstrUID, CONNECT_UPDATE_PROFILE)
If lngRet = 0 Then
TextBox1.Text = “Connected”
Else
TextBox1.Text = “Disconnect”
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Cn As New OleDbConnection(”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\xxx\xxx.mdb”) ‘今回はmdbを操作
Dim SQLCm As OleDbCommand = Cn.CreateCommand
Dim Adapter As New OleDbDataAdapter(SQLCm)
Dim Table As New DataTable
SQLCm.CommandText = “SELECT * FROM xxx ORDER BY xxx.xxx”
Adapter.Fill(Table)
DataGrid1.DataSource = Table
‘実作業
Table.Dispose()
Adapter.Dispose()
SQLCm.Dispose()
Cn.Dispose()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim RmHost As String = “\\サーバーアドレス\フォルダ”
Dim RmUID As String = “ユーザー名”
Dim RmPWD As String = “パスワード”
ConnectSrv(RmHost, RmUID, RmPWD)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim result As Boolean
result = WNetCancelConnection(”Z:”, CONNECT_UPDATE_PROFILE, True)
Debug.WriteLine(result.ToString())
If result = “0″ Then
TextBox1.Text = “Disconnect”
Else
TextBox1.Text = “Connected”
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
WNetCancelConnection(”Z:”, CONNECT_UPDATE_PROFILE, True)
Me.Close()
End Sub
Leave a Reply
*----* Note : *----*
Please write only in Japanese or English.
If you will post any other languages, we'll mark them as spam.
And also if a linked URI in your comments, I do not approve.
*----*----*
You must be logged in to post a comment.
3月 18th, 2009 on 1:05 pm
Hello,
Ugh, I liked! So clear and positively.
Have a nice day
Pett
3月 22nd, 2009 on 4:54 pm
Hi,
Not sure that this is true:), but thanks for a post.
Thanks
Saurooon
3月 24th, 2009 on 12:37 pm
Hi,
worldstep.net to GoogleReader!
Thank you
AlexAxe
4月 16th, 2009 on 12:17 am
thanks !! very helpful post!
4月 17th, 2009 on 8:32 am
Good post!
5月 13th, 2009 on 10:35 pm
Great point and very interesting food for thought. I’m not sure I have any clients I can replicate this with, but will bear in mind for the future. Regards
5月 14th, 2009 on 1:20 pm
Hello,
worldstep.net - da best. Keep it going!
7月 9th, 2009 on 4:38 am
I want to say - thank you for this!
7月 24th, 2009 on 7:45 pm
The best information i have found exactly here. Keep going Thank you
7月 31st, 2009 on 5:41 am
Very nice site!
8月 7th, 2009 on 1:21 am
Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.
4月 29th, 2010 on 9:32 pm
You built some excellent points there. I did a search on the topic and discovered many people will agree with your weblog.
5月 17th, 2010 on 3:42 am
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
5月 17th, 2010 on 8:30 pm
Very informative post. Thanks for taking the time to share your view with us.
7月 22nd, 2010 on 4:38 am
I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.
7月 30th, 2010 on 2:36 am
First time at your blog and I really enjoyed reading it. I will come back again in the future to check out some of the other articles.
8月 5th, 2010 on 4:28 pm
nice articles thank you for sharing
8月 5th, 2010 on 6:56 pm
good information you
write it very clean. I am very lucky to get this tips from you.
10月 20th, 2015 on 8:43 am
Hello, I enjoy reading all of your article. I like to write a little comment to support you….