locked
connect to directory, another pc RemoteAccessHelper.NetworkConnection RRS feed

  • Question

  • Hello,


    I am trying to connect to another computer in same domain to download files. i use

     
                    string m_rep = null;
                    m_rep = @"\\"+p_Ip+@"\c$\toli\rel\out";
                    var oNetworkCredential = new System.Net.NetworkCredential() { Domain = p_Ip, UserName = @"\\" + p_Ip + @"\c$\" + "user", Password = "pwd" };
                    using (new RemoteAccessHelper.NetworkConnection(@"\\" + p_Ip+@"\c$", oNetworkCredential))
                    {
                        foreach (var v_MyFile in Directory.GetFiles(m_rep, m_fichierDuJour))

    this code works for the greater part of the computers, but for some computers i have an error {"Error connecting to remote share"}.

    I dont undestand because i can connect with windows explorer.

     


    any ideas?


    ty


    "Le temps c'est de la monnaie"




    • Edited by 3NIMA Friday, November 27, 2015 9:49 AM
    Friday, November 27, 2015 9:47 AM

Answers

  • Hi 3NIMA,

    I've checked that class named RemoteAccessHelper, it is not a class that belongs to .Net Framework class libraries.

    Here is what I found about RemoteAccessHelper class.

    >>Based on the error:{"Error connecting to remote share"}.

    The related WNetAddConnection2 function makes a connection to a network resource. The function can redirect a local device to the network resource.

    Please check wnetaddconnection2 (mpr) for more information.

    I also suggest you compare worked machine with failed machine, I suppose that WNetAddConnection2 function dose not support a specific environment.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System.Net;
    
    namespace SomeNamespace.Utility
    {
        public class RemoteAccessHelper
        {
            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
            }
        }
    }
    

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Monday, December 7, 2015 7:39 AM
    • Marked as answer by DotNet Wang Tuesday, December 8, 2015 1:41 AM
    Monday, November 30, 2015 6:05 AM