Re: Netzlaufwerk verbinden und trennen



Hallo Arnd,

hier eine Möglichkeit wo gleichzeitig noch geprüft wird ob das Netzlaufwerk
schon verbunden
ist (mit Schreibrechten auf diesem):

#Region " Network via API "

Private Const CONNECT_INTERACTIVE As Long = &H8

Private Const RESOURCETYPE_DISK As Long = &H1

Friend Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _

"WNetAddConnection2A"
(<Runtime.InteropServices.MarshalAs(UnmanagedType.Struct)> ByRef
lpNetResource _

As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, _

ByVal dwFlags As Integer) As Integer

<StructLayout(LayoutKind.Sequential)> 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 Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _

"WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As _

Integer, ByVal fForce As Integer) As Integer

Public Shared Function NetConnect(ByVal path As String, ByVal user As
String, ByVal password As String) As Boolean

Try

Dim w As StreamWriter = File.CreateText(path & "\test.txt")

w.WriteLine("test")

w.Flush()

w.Close()

File.Delete(path & "\test.txt")

Return True

Catch ex As Exception

End Try



Dim nr As New NETRESOURCE()

nr.dwType = RESOURCETYPE_DISK

nr.lpRemoteName = path

If WNetAddConnection2(nr, password, user, CONNECT_INTERACTIVE) = 0 Then

Return True

End If

End Function

Public Shared Sub NetDisConnect(ByVal path)

Dim nr As New NETRESOURCE()

nr.dwType = RESOURCETYPE_DISK

nr.lpRemoteName = path

WNetCancelConnection2(path, 0, -1)

End Sub

#End Region

Gruß

Jens




.



Relevant Pages

  • Re: OLE Object- the real question
    ... pszDisplayName As String ... Private Const BIF_RETURNONLYFSDIRS = &H1 ... Dim x As Long, bi As BROWSEINFO, dwIList As Long ... >> Dim strFilename As String, ...
    (microsoft.public.access.forms)
  • Re: Set Path for Folder Picker
    ... It can handle any file or folder dialog. ... ByVal lpWindowName As String) As Long ... Private Declare Function lstrlen Lib "kernel32" _ ... Private Const OFN_ALLOWMULTISELECT As Long = &H200 ...
    (microsoft.public.vb.general.discussion)
  • Re: Format Excel - Object required error
    ... tell excel to autofit the columns. ... Public Sub FormatXLReport(strPath As String, ... Dim blnExcelExists As Boolean ... Private Const SW_SHOWNORMAL = 1 ...
    (microsoft.public.access.modulesdaovba)
  • Re: VB6 Opens Excel, Saves File, Then Closes Excel
    ... Private Const xlUnderlineStyleNone = -4142 ... Public Function Excel_Link(strExcelTemplateName As String) As Boolean ... Dim intLoop As Integer ... ' This Property returns the length of the open file. ...
    (microsoft.public.vb.general.discussion)
  • Re: Is there a Folder Picker method that shows files too?
    ... ByVal lpWindowName As String) As Long ... Private Declare Function lstrlen Lib "kernel32" _ ... Private Const OFN_ALLOWMULTISELECT As Long = &H200 ... PickFileFolder = BuildCSVMultiString ...
    (microsoft.public.excel.programming)

Loading