Get shared network file using .net

Tuesday, 19 August 2014

Get shared network file using .net

Copy network file using .net

In this project i want to copy some file from one machine to another machine  using .net technology .Details code below :

Imports System.Security.Principal
Imports System.IO
Imports System.Runtime.InteropServices


  <DllImport("advapi32.DLL", SetLastError:=True)> _
    Public Shared Function LogonUser(lpszUsername As String, lpszDomain As String, lpszPassword As String, dwLogonType As Integer, dwLogonProvider As Integer, ByRef phToken As IntPtr) As Integer
    End Function
    Private Shared Sub CopyToSharedFolder()
        Dim admin_token As IntPtr = Nothing
        Dim wid_current As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim wid_admin As WindowsIdentity = Nothing
        Dim wic As WindowsImpersonationContext = Nothing

        Try
        
            Dim a As String = LogonUser("userid", "IP address", "password", 9, 0, admin_token).ToString
            If LogonUser("userid", "IP address", "password",9, 0, admin_token) <> 0 Then
                wid_admin = New WindowsIdentity(admin_token)
                wic = wid_admin.Impersonate()
             
               System.IO.File.Copy("Sourcepath ", "Destination path ", True)
          

            End If
        Catch se As System.Exception
            Dim ret As Integer = Marshal.GetLastWin32Error()
        Finally
            If wic IsNot Nothing Then
                wic.Undo()
            End If
        End Try
    End Sub