Exemplo n.º 1
0
        private void btnAddUsers_Click(object sender, RoutedEventArgs e)
        {
            lblUploadStatus.Content = "";
            //Open the file and add users and groups to list
            try
            {
                var           reader = new StreamReader(File.OpenRead(txtFileLocation.Text));
                List <string> users  = new List <string>();
                List <string> groups = new List <string>();
                while (!reader.EndOfStream)
                {
                    var line   = reader.ReadLine();
                    var values = line.Split(',');

                    users.Add(values[0]);
                    groups.Add(values[1]);
                }

                //for each user add them to the project and respective group
                bool errorOccured = false;
                int  i            = 0;
                foreach (string user in users)
                {
                    //Try to create the user, log error if any
                    try{
                        sapi.CreateUserEx(user, "", "", "", "", "", "");
                    }
                    catch (COMException) {
                        using (StreamWriter w = File.AppendText(GetDirectory() + "log.txt")){
                            w.WriteLine("Warning: " + user + " may already exist in the System");
                        }
                    }

                    //Try to add user to project, log error if any
                    try
                    {
                        sapi.AddUsersToProject(drpDomain.SelectedValue.ToString(), drpProject.SelectedValue.ToString(), user);
                    }
                    catch (COMException)
                    {
                        using (StreamWriter w = File.AppendText(GetDirectory() + "log.txt"))
                        {
                            w.WriteLine("Error adding user " + user + " to " + drpProject.SelectedValue.ToString());
                            errorOccured = true;
                        }
                    }

                    //try to add user to the group, log error if any
                    try
                    {
                        sapi.AddUsersToGroup(drpDomain.SelectedValue.ToString(), drpProject.SelectedValue.ToString(), groups.ElementAt(i), user);
                    }
                    catch (COMException)
                    {
                        using (StreamWriter w = File.AppendText(GetDirectory() + "log.txt"))
                        {
                            w.WriteLine("Error adding " + user + " to " + groups.ElementAt(i));
                            errorOccured = true;
                        }
                    }
                    i++;
                }
                reader.Close();

                //notify user if error occured, otherwise show task complete
                if (errorOccured)
                {
                    lblUploadStatus.Content = "Error Detected, Check log file.";
                }
                else
                {
                    lblUploadStatus.Content = "Uploading Task Complete!";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Could not open file at " + txtFileLocation.Text + "\nMake sure the file exists and you have access\nto the directory.");
            }
        }