private void btnRemove_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in this.dgvCredentials.SelectedRows) { Models.NetworkCredential credential = (Models.NetworkCredential)row.DataBoundItem; bool isNew = Added.Contains(credential); if (isNew) { Added.Remove(credential); } else { Removed.Add(credential); } if (Modified.Contains(credential)) { Modified.Remove(credential); } dgvCredentials.Rows.RemoveAt(row.Index); All.Remove(credential); } }
private void EditRow(DataGridViewRow row) { Models.NetworkCredential credential = (Models.NetworkCredential)row.DataBoundItem; Models.NetworkCredential untouchedCredential = credential.Clone() as Models.NetworkCredential; using (var form = new AddEditNetworkCredentialForm(credential)) { form.Canceled += (object sender2, NetworkCredentialActionEventArgs e2) => { // Revert changes. credential.RevertTo(untouchedCredential); }; form.Confirmed += (object sender2, NetworkCredentialActionEventArgs e2) => { bool isNew = Added.Contains(credential); if (!isNew) { Modified.Add(e2.Credential); dgvCredentials.InvalidateRow(row.Index); } }; form.ShowDialog(this); } }
public AddEditNetworkCredentialForm(Models.NetworkCredential credential) { InitializeComponent(); Model = credential; drivesBindingSource1.BindData(); WireBindings(); }
private void MountNetworkDrive(Models.NetworkCredential cred) { string message = null; try { DriveInfo drive = GetDriveInfo(cred.MountPoint); if (drive != null) { string userName = null; string remotePath = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint); string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint); message = string.Format("{0} is already mounted to {1} by {2}", cred.MountPoint, remotePath, credentialUsed); logger.Info(message); // Was this mounted using the same credential? // We assume the mount point is OK if we cannot retrieve the username that originally mounted it. if (userName != null && (userName.Equals(cred.Login, StringComparison.InvariantCulture) || userName.Equals(MappedDriveResolver.UNKNOWN_CREDENTIAL, StringComparison.InvariantCulture))) { return; // We're OK then. } NetworkDriveMapper.UnmountNetworkLocation(cred.MountPoint); logger.Info("Umounted {0}", cred.MountPoint); } NetworkDriveMapper.MountNetworkLocation(cred.MountPoint, cred.Path, cred.Login, cred.Password, false); message = string.Format("Successfully mounted {0} to {1} as {2}", cred.MountPoint, cred.Path, cred.Login); logger.Info(message); } catch (Win32Exception ex) { string reason = ex.Message; switch (ex.NativeErrorCode) { case NetworkDriveMapper.ERROR_ALREADY_ASSIGNED: { //string userName = null; string remotePath = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint); string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint); reason = string.Format("It's already mounted to {0} by {1}", remotePath, credentialUsed); break; } } message = string.Format("Failed to mount {0} to {1} as {2} - {3}", cred.MountPoint, cred.Path, cred.Login, reason); throw new FailedToMountNetworkDrive(message); } }
private void btnAdd_Click(object sender, EventArgs e) { Models.NetworkCredential credential = new Models.NetworkCredential(); using (var form = new AddEditNetworkCredentialForm(credential)) { form.Canceled += (object sender2, NetworkCredentialActionEventArgs e2) => { // Do nothing. }; form.Confirmed += (object sender2, NetworkCredentialActionEventArgs e2) => { Added.Add(e2.Credential); All.Add(e2.Credential); RefreshDataGrid(); }; form.ShowDialog(this); } }