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);

            try
            {
                var result = WNetAddConnection2(
                    netResource,
                    credentials.Password,
                    userName,
                    0);
            }

            catch(Win32Exception e)
            {
                MessageBox.Show("Error connecting to the remote share.\n" + e.Message);
            }
        }
 private static extern int WNetAddConnection2(NetResource netResource,
     string password, string username, int flags);