.NET winforms file copy....do i need impersonation

samrose
samrose used Ask the Experts™
on
Hi Experts,

I'm trying to copy a file off my local pc to another computer on a different domain.

I have a username and password for the remote pc and I can ping it (VPN already setup)

I was wondering how I copy this file...as I can't figure it out.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I believe you do indeed have to connect to the share using credentials.

A snippet from a powershell script I'm working on does this:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive('x:', $shareUrl, $false, $shareUser, $sharePassword)

So if you do a search for WScript.Network and MapNetworkDrive I'm sure you'll find a vb.net example.

Then it's a question of productionizing it. For instance, checking if the drive's already mapped, disconnecting, etc.

I enumerate the drive mappings, looking for "X" and if not found, map it:

if (($net.EnumNetworkDrives() | where { $_ -eq 'x:' }) -eq $null)

I know this isn't vb.net but again, the method names should get you moving in no time.
Commented:
look at the snippet i have 2 classes
you can use

Drivletter="X"
UNXPath="\\server\transfer$"
Username="USER"
Password="PWD"

MapDrive(Drivletter,UNXPath,Username,Password)

Imports System.Runtime.InteropServices
Class WNet
    <StructLayout(LayoutKind.Sequential)> _
    Public Class 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 Class
    Public Declare Ansi Function WNetAddConnection2 Lib "mpr.dll" Alias _
    "WNetAddConnection2A" (ByVal lpNetResource As NETRESOURCE, ByVal lpPassword _
    As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
    "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, _
    ByVal fForce As Long) As Integer
End Class
 
Imports System.Runtime.InteropServices
Module modGetServerPathAccess
    Public _DATABASE As String
    Public _INSTANCE As String
    Public _USER As String
    Public _PWD As String
    Public _BACKUPFILE As String
    Const RESOURCETYPE_DISK = &H1
    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String) As Boolean
        Dim theNetResource As WNet.NETRESOURCE
        Dim result As Integer
        theNetResource = New WNet.NETRESOURCE
        theNetResource.lpRemoteName = UNCPath
 
        theNetResource.lpLocalName = DriveLetter & ":"
 
        theNetResource.dwType = RESOURCETYPE_DISK
 
        result = WNet.WNetAddConnection2(theNetResource, strPassword, strUsername, 0)
 
        If result = 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Function UnMapDrive(ByVal DriveLetter As String) As Integer
        Dim rc As Integer
        Dim theNetResource As WNet.NETRESOURCE
        Dim result As Integer
        theNetResource = New WNet.NETRESOURCE
        rc = WNet.WNetCancelConnection2(DriveLetter & ":", 0, 0)
        Return rc
    End Function
End Module

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial