Take it easy!

ファイルサーバー接続&ドライブ割り当て

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

19 comments for this entry:
  1. Pett

    Hello,
    Ugh, I liked! So clear and positively.

    Have a nice day
    Pett

  2. Saurooon

    Hi,
    Not sure that this is true:), but thanks for a post.

    Thanks
    Saurooon

  3. AlexAxe

    Hi,
    worldstep.net to GoogleReader!

    Thank you
    AlexAxe

  4. mark

    thanks !! very helpful post!

  5. ArianaRese

    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

  6. Pett

    Hello,
    worldstep.net - da best. Keep it going!

  7. personal review cialis

    I want to say - thank you for this!

  8. LnddMiles

    The best information i have found exactly here. Keep going Thank you

  9. John1449

    Very nice site!

  10. Extenze

    Super-Duper site! I am loving it!! Will come back again - taking you feeds also, Thanks.

  11. masonry stove

    You built some excellent points there. I did a search on the topic and discovered many people will agree with your weblog.

  12. vemma

    I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

  13. lcd reviews

    Very informative post. Thanks for taking the time to share your view with us.

  14. vemmaac

    I just book marked your blog on Digg and StumbleUpon.I enjoy reading your commentaries.

  15. Fat Loss 4 Idiots

    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.

  16. wow gold

    nice articles thank you for sharing

  17. registry cleaner reviews

    good information you

    write it very clean. I am very lucky to get this tips from you.

  18. oakley

    Hello, I enjoy reading all of your article. I like to write a little comment to support you….

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.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...