C#

[DllImport("mpr.dll", EntryPoint = "WNetAddConnectionA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern long WNetAddConnection(string lpszNetPath, string lpszPassword, string lpszLocalName);

[DllImport("mpr.dll", EntryPoint = "WNetCancelConnectionA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern long WNetCancelConnection(string lpszName, long bForce);

//無密碼可傳空字串
public long AddConnection(string 路徑, string 登入密碼, string 磁碟機代號)
{
    try
    {
        return WNetAddConnection(路徑, 登入密碼, 磁碟機代號);
    }
    catch (Exception ex)
    {
        DebugMsg(ex.Message.ToString());
        return 0;
    }
}

//Force: 0 如有檔案開啟中則傳回ERROR_OPEN_FILES, 1 強制關閉
//ERROR_OPEN_FILES: There are open files, and the fForce parameter is FALSE.
public long CancelConnection(string 磁碟機代號, int Force)
{
    try
    {
        return WNetCancelConnection(磁碟機代號, Force);
    }
    catch (Exception ex)
    {
        DebugMsg(ex.Message.ToString());
        return 0;
    }
}

VB

Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" _
(ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName _
As String) As Long

Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" _
(ByVal lpszName As String, ByVal bForce As Long) As Long

Function AddConnection(ByVal 路徑 As String, ByVal 登入密碼 As String, ByVal 磁碟機代號 As String) As Long
    Try
        Return WNetAddConnection(路徑, 登入密碼, 磁碟機代號)
    Catch ex As Exception
        DebugMsg(ex.Message.ToString())
    End Try
End Function

Function CancelConnection(ByVal 磁碟機代號 As String, ByVal Force As Integer) As Long
    Try
        Return WNetCancelConnection(磁碟機代號, Force)
    Catch ex As Exception
        DebugMsg(ex.Message.ToString())
    End Try
End Function

arrow
arrow
    文章標籤
    c# vb
    全站熱搜
    創作者介紹
    創作者 伊 的頭像

    伊のspace~芳香精油*美容保養*程式設計

    伊 發表在 痞客邦 留言(0) 人氣()