0

I have a requirement in which i have to transfer a file from local client machine to a virtual directory of a different machine.

I am able to browse the virtual directory of the host machine and view all the files in the directory in the client. File transfer will be from local c:\test.txt to http: //myserver:11211/VirtualDirectory

Rahul
  • 26
  • 8
  • 1
    Did you try anything? – Nicolas R Jul 09 '14 at 13:21
  • yes i tried using File.Copy but it errors Invalid URI format for the virtual directory of the server. I tried Webclient.UploadFile and getting error. :( – Rahul Jul 09 '14 at 13:25
  • Please share the code of what you tried. We don't even know what kind of application you intend to use: windows application, console application, WPF, WCF... – Nicolas R Jul 09 '14 at 13:29
  • Its a window application. private void button1_Click(object sender, EventArgs e) { WebClient client = new WebClient(); string myFile = textBox1.Text; string destFile = @"http://myserver:11711/VirtualDirectory/"; try { Uri localPath = new Uri(destFile);; client.UploadFile(destFile, "PUT", myFile); } catch(Exception ex) { label1.Text=ex.Message; } client.Dispose(); } – Rahul Jul 09 '14 at 14:05

1 Answers1

0

Can you try using simply File.Copy included in a network connection used like this:

using (new NetworkConnection(@"\\myserver:11211\VirtualDirectory", writeCredentials)) {
   File.Copy(@"c:\test.txt", @"\\myserver:11211\VirtualDirectory\test.txt");
}

With this NetworkConnection class:

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 userName = string.IsNullOrEmpty(credentials.Domain)
            ? credentials.UserName
            : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

        var result = WNetAddConnection2(
            netResource, 
            credentials.Password,
            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
}

See this SO question for more details: How to provide user name and password when connecting to a network share

Community
  • 1
  • 1
Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • I used the above code...but getting Error connecting to remote share. And i sure that i am using correct credential and the URL, but still getting the error. I believe we cannot open the virtual directory using \\ instead of http:// – Rahul Jul 10 '14 at 10:53
  • http is not the good protocol for your file server transfer. Here you are in a domain context. Can you paste the detailed exception you got? – Nicolas R Jul 11 '14 at 08:32
  • "Error connecting to remote share" - This error is a custom error from the code which you have provided. – Rahul Jul 11 '14 at 14:19
  • What kind of credentials did you pass? What is the result you got before the Win32Exception was thrown? – Nicolas R Jul 11 '14 at 14:27