I am not expert. I am trying to delete the files from the directory at button click. I have windows application which have folders populated in combobox1 and files populated in combobox2. I need to delete the selected folder from combobox1 and files within the folder. I am getting error cannot delete the file as it's being used by another process. Basically application can be used by multiple users and one of the folder I am trying to delete might be listed on the second users combobox. I need to delete the files even if its listed in second users combobox, as I have another logic that doesn't allow the user to selecte the combobox item selected by first user.

 {
                int selindex02 = 0;
                string batchrem = "";
                selindex02 = comboBox1.SelectedIndex;
                batchrem = comboBox1.Text;

                string dir_name = comboBox1.Text.ToString().Substring(0, 12);
                string path1 = strval + dir_name + "\\";
                //string path1 ="C:\\Project\\" + dir_name + "\\";
                DirectoryInfo di = new DirectoryInfo(path1);
                SqlConnection conn001 = new SqlConnection();
                conn001.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
                SqlCommand comd012 = new SqlCommand("GetBatch", conn001);
                comd012.CommandType = CommandType.StoredProcedure;
                comd012.CommandText = "usp_CAMR_Delete_MarriageInfo";
                comd012.Connection = conn001;
                comd012.Parameters.AddWithValue("@batch_name", comboBox1.Text.ToString().Substring(0, 12));
                comd012.Parameters.AddWithValue("@ERR_CODE", 0);
                comd012.Parameters.AddWithValue("@ERR_MSG", 0);
                comd012.Parameters.AddWithValue("@TABLE_NAME", "");
                try
                {
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
                    SqlCommand cmd = new SqlCommand("BatchUpdate", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "usp_CAMR_UnlockedBatchName";
                    //cmd.Connection = con;
                    con.Open();
                    cmd.Parameters.AddWithValue("@Batch_Name", comboBox1.Text.ToString().Substring(0, 12));
                    cmd.Parameters.AddWithValue("@lst_mod_userid", Environment.UserName);
                    cmd.Parameters.AddWithValue("@ERR_CODE", 0);
                    cmd.Parameters.AddWithValue("@ERR_MSG", 0);
                    cmd.Parameters.AddWithValue("@TABLE_NAME", 0);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    con.Dispose();
                    conn001.Open();
                    comd012.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    lblerror.Text = ex.Message;
                }
                finally
                {
                    conn001.Close();
                    conn001.Dispose();
                    if (di.Exists)
                    {
                        try
                        {
                            if (comboBox1.SelectedIndex < comboBox1.Items.Count - 1)
                            {
                                comboBox1.SelectedIndex = selindex02 + 1;
                            }
                            else
                            {
                                // comboBox1.Items.Remove(batchrem);
                                comboBox1.SelectedIndex = 0;
                            }
                            foreach (FileInfo fi in di.GetFiles())
                            {

                                System.GC.Collect();
                                System.GC.WaitForPendingFinalizers();
                                fi.IsReadOnly = false;
                                fi.Delete();
                            }
                            di.Delete();
                            comboBox1.Items.Remove(batchrem);
                        }
                        catch (Exception ex)
                        {
                            {
                                oErrorLog.WriteErrorLog(ex.ToString());
                            }
                            lblerror.Text = "Please contact support team";
                        }
                    }
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.