Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,
I am quite new to this type of project i am tackling.
I am developing a small project to transfer files from one machine to another.
I know how to use the File.Copy command, but these computers will not always be on the same workgroup, and might have different username's & passwords(so the sharing is not always direct)
I have tried searching but with no luck.
I would also like both machines files to show in listview.
I want to keep it very simple, i have something in mind like UltraVNC's file transfer.(1 form, drag & drop)
Your help/ideas will be appreciated.
Thanks
Posted

It is possible to access a remote machine with PowerShell, but you need the correct applicaiton on the remote machine. Your other option is to create a service on the remote machine to communicate with that will provide the required information. You can run powershell from c#. See How to run PowerShell scripts from C#[^]

Powershell is a great tool that can run dos and Unix commands (maybe not all), and works with objects instead of strings. Commands will return an object that contains the infroamtion, so when you do a dir, you do not get strings back, but objects that contains the detailed information.
 
Share this answer
 
v2
You can use File.Copy even across domains (workgroups). You only need to have the needed credentials (username & password). As I know there is no direct implementation needed in .NET only, in the API.

I found this code for you:
C#
public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkName, 
        NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource()
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

        var result = WNetAddConnection2(
            netResource, 
            credentials.Password,
            credentials.UserName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result, "Error connecting to remote share");
        }   
    }

    ~NetworkConnection()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        WNetCancelConnection2(_networkName, 0, true);
    }

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource, 
        string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags,
        bool force);
}

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplaytype DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

public enum ResourceScope : int
{
    Connected = 1,
    GlobalNetwork,
    Remembered,
    Recent,
    Context
};

public enum ResourceType : int
{
    Any = 0,
    Disk = 1,
    Print = 2,
    Reserved = 8,
}

public enum ResourceDisplaytype : int
{
    Generic = 0x0,
    Domain = 0x01,
    Server = 0x02,
    Share = 0x03,
    File = 0x04,
    Group = 0x05,
    Network = 0x06,
    Root = 0x07,
    Shareadmin = 0x08,
    Directory = 0x09,
    Tree = 0x0a,
    Ndscontainer = 0x0b
}

It can be used to impersonate somebody during the file transfer. As you can see, you even can impersonate two different accounts.

C#
using (new NetworkConnection(@"\\server\read", readCredentials))
using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
   File.Copy(@"\\server\read\file", @"\\server2\write\file");
}

Of course, you can use admin shares, no need for real shares, but you need admin account for that. You could store the different credentials in your application setting (encrypted, if needed), or you could add a common username-password pair on all affected machines.

By the way, you can even use wmi to copy files (as psexec does), but it seems to me more complicated.
 
Share this answer
 
Comments
jpveldtman 23-May-12 4:26am    
Thanks, Could you please just assist me in the readCredentials & writeCredentials?
How do i declare it?
Thanks in advance
Zoltán Zörgő 23-May-12 7:11am    
These are both instances of this class: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx
readCredential is the account you use on the source machine, writeCredential is for the target machine. Of course, you don't need the first one if you copy from the machine running the application.
Zoltán Zörgő 27-May-12 13:39pm    
Any progress?
Sorry, was out of the country for a while, used Filestream with a BinaryWriter in the end, displaying a progress in a new window, thanks for your help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900