Share via


How to connect and access a drive in remote server from my local machine using credentials in C#.NET console application?

Question

Monday, July 28, 2014 11:08 AM

Hi,

       I'm Newbie for .NET framework. I'm developing one console application.

By this APP, I'm trying to connect the remote server with credential.

Once server connected, I would like to access the drive (like G:\ and fetch some files from there.

How is it possible in C#.NET? Can anyone assist me?

Thanks in advance!!!

SathyJay

All replies (4)

Wednesday, July 30, 2014 3:02 AM ✅Answered | 1 vote

Hi Sathyjay,

Based on you said. The way to solve your problem is to use a Win32 API called WNetUseConnection.

The WNetUseConnection function makes a connection to a network resource. The function can redirect a local device to a network resource.This will allow you to connect to a remote machine, even if it is not on the same domain, and even if it has a different username and password.

For connect server code.

public class NetworkConnection : IDisposable
    {
        private string _networkName;
        public NetworkConnection(string networkName, NetworkCredential credentials)
        {
            _networkName = networkName;

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

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

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

        ~NetworkConnection()
        {
            Dispose(false);
        }

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

        protected 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 = 0x1,
        Server = 0x2,
        Share = 0x3,
        File = 0x4,
        Group = 0x5,
        Network = 0x6,
        Root = 0x7,
        Shareadmin = 0x8,
        Directory = 0x9,
        Tree = 0xa,
        Ndscontainer = 0xb
    }

>>I would like to access the drive (like G:\ and fetch some files from there.

Once you have connected to the server, you will be able to access G:\ and fetch files.

Have a nice day!

Kristin


Monday, July 28, 2014 11:29 AM

Hi,

take a look here: http://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials


Monday, July 28, 2014 4:33 PM | 1 vote

If the <G> is mapped to a correctly, then it's just a matter of knowing the location on <G> where the file is located, as in the example where the <C> is being used.

http://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.110).aspx


Sunday, October 22, 2017 7:10 PM

when to use this code instead of simply using :

NetworkCredential theNetworkCredential = newNetworkCredential(username, password, domain);
CredentialCache theNetcache = new CredentialCache();
theNetCache.Add(URL, theNetworkCredential, "Basic", theNetworkCredential); ??