private void cm_OnConnectToShare(object sender, EventArgs e)
        {
            WinError error = WinError.ERROR_SUCCESS;
            bool determinePath = true;
            bool initialConnect = false;
            bool showPathError = false;
            string username = null;
            string path = null;

            while (true)
            {
                if (determinePath)
                {
                    // Determine share path to connect to
                    ConnectToShareDialog connectDialog = new ConnectToShareDialog(path, showPathError);

                    if (connectDialog.ShowDialog() == DialogResult.OK)
                    {
                        showPathError = false;
                        determinePath = false;
                        path = connectDialog.GetPath();

                        if (connectDialog.UseAlternateUserCreds())
                        {
                            error = WinError.ERROR_ACCESS_DENIED;
                        }
                        else
                        {
                            initialConnect = true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (initialConnect)
                {
                    Application.UseWaitCursor = true;
                    error = FileClient.FileClient.CreateConnection(path, null, null);
                    Application.UseWaitCursor = false;
                    initialConnect = false;
                }

                if (error == WinError.ERROR_SUCCESS)
                {
                    // Refresh RemoteShares list and exit
                    RemoteShares.Add(path);
                    break;
                }

                if (error == WinError.ERROR_BAD_NET_NAME)
                {
                    // Show share path connect dialog to allow the user to correct the bad path
                    //MessageBox.Show("The network path is unavailable", "File connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    showPathError = true;
                    determinePath = true;
                    continue;
                }

                if (error == WinError.ERROR_DOWNGRADE_DETECTED ||
                    error == WinError.ERROR_ACCESS_DENIED ||
                    error == WinError.ERROR_SESSION_CREDENTIAL_CONFLICT ||
                    error == WinError.ERROR_BAD_USERNAME ||
                    error == WinError.ERROR_INVALID_PASSWORD ||
                    error == WinError.ERROR_LOGON_TYPE_NOT_GRANTED)
                {
                    // Prompt for updated user credentials to access share
                    CredentialsDialog credDialog = new CredentialsDialog(username);

                    if (credDialog.ShowDialog() == DialogResult.OK)
                    {
                        if (credDialog.UseDefaultUserCreds())
                        {
                            initialConnect = true;
                            username = null;
                            continue;
                        }

                        username = credDialog.GetUsername();

                        Application.UseWaitCursor = true;
                        error = FileClient.FileClient.CreateConnection(path,
                                                                       username,
                                                                       credDialog.GetPassword());
                        Application.UseWaitCursor = false;
                        continue;
                    }
                    else
                    {
                        // Cancel Connect To attempt
                        break;
                    }
                }
                else
                {
                    // Encounter unexpected error
                    break;
                }
            }

            RefreshNetworkTreeNode();
            Application.UseWaitCursor = false;
        }
Пример #2
0
        private void cm_OnConnectToShare(object sender, EventArgs e)
        {
            WinError error          = WinError.ERROR_SUCCESS;
            bool     determinePath  = true;
            bool     initialConnect = false;
            bool     showPathError  = false;
            string   username       = null;
            string   path           = null;

            while (true)
            {
                if (determinePath)
                {
                    // Determine share path to connect to
                    ConnectToShareDialog connectDialog = new ConnectToShareDialog(path, showPathError);

                    if (connectDialog.ShowDialog() == DialogResult.OK)
                    {
                        showPathError = false;
                        determinePath = false;
                        path          = connectDialog.GetPath();

                        if (connectDialog.UseAlternateUserCreds())
                        {
                            error = WinError.ERROR_ACCESS_DENIED;
                        }
                        else
                        {
                            initialConnect = true;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (initialConnect)
                {
                    Application.UseWaitCursor = true;
                    error = FileClient.FileClient.CreateConnection(path, null, null);
                    Application.UseWaitCursor = false;
                    initialConnect            = false;
                }

                if (error == WinError.ERROR_SUCCESS)
                {
                    // Refresh RemoteShares list and exit
                    RemoteShares.Add(path);
                    break;
                }

                if (error == WinError.ERROR_BAD_NET_NAME)
                {
                    // Show share path connect dialog to allow the user to correct the bad path
                    //MessageBox.Show("The network path is unavailable", "File connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    showPathError = true;
                    determinePath = true;
                    continue;
                }

                if (error == WinError.ERROR_DOWNGRADE_DETECTED ||
                    error == WinError.ERROR_ACCESS_DENIED ||
                    error == WinError.ERROR_SESSION_CREDENTIAL_CONFLICT ||
                    error == WinError.ERROR_BAD_USERNAME ||
                    error == WinError.ERROR_INVALID_PASSWORD ||
                    error == WinError.ERROR_LOGON_TYPE_NOT_GRANTED)
                {
                    // Prompt for updated user credentials to access share
                    CredentialsDialog credDialog = new CredentialsDialog(username);

                    if (credDialog.ShowDialog() == DialogResult.OK)
                    {
                        if (credDialog.UseDefaultUserCreds())
                        {
                            initialConnect = true;
                            username       = null;
                            continue;
                        }

                        username = credDialog.GetUsername();

                        Application.UseWaitCursor = true;
                        error = FileClient.FileClient.CreateConnection(path,
                                                                       username,
                                                                       credDialog.GetPassword());
                        Application.UseWaitCursor = false;
                        continue;
                    }
                    else
                    {
                        // Cancel Connect To attempt
                        break;
                    }
                }
                else
                {
                    // Encounter unexpected error
                    break;
                }
            }

            RefreshNetworkTreeNode();
            Application.UseWaitCursor = false;
        }