用于选择目录然后删除其中所有文件的SQL查询- c#

本文关键字:文件 SQL 查询 选择 然后 删除 用于 | 更新日期: 2023-09-27 18:05:11

我需要删除目录中的所有文件(由于画廊脚本保持全尺寸图像但不使用它们!)。

我写了一段代码,将查询转储到DataTable中,然后循环遍历记录。然而,当我引入查找所有文件并删除它们的循环时,它似乎只对第一个SiteID运行。应该至少有350个目录要扫描,但它只扫描第一个SiteID(这个SiteID中的所有模块)。

我被难住了,有什么想法吗?

private void frmMain_Load(object sender, EventArgs e)
{
    SqlConnection con = null;
    try
    {
        // Open connection to the database
        string ConnectionString = "server=(local);UID=x;PWD=y;database=mojoportal2";
        con = new SqlConnection(ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        da.SelectCommand = new SqlCommand(@"SELECT SiteID, ModuleID FROM mp_Modules where ModuleDefID = 16 ORDER By SiteID DESC", con);
        da.Fill(ds, "Sites");
        dt = ds.Tables["Sites"];
        foreach (DataRow dr in dt.Rows)
        {
            string SiteID = dr["SiteID"].ToString();
            string ModuleID = dr["ModuleID"].ToString();
            string directoryPath = @"E:'Website'" + SiteID + @"'media'GalleryImages'" + ModuleID + @"'FullSizeImages";
            MessageBox.Show("Deleting Files In " + directoryPath);
            string[] files = Directory.GetFiles(directoryPath);
            string[] dirs = Directory.GetDirectories(directoryPath);
            foreach (string file in files)
            {
                MessageBox.Show(file);
                //File.SetAttributes(file, FileAttributes.Normal);
                //File.Delete(file);
            }
        }
    }
    catch (Exception ex)
    {
        // Print error message
        MessageBox.Show(ex.Message);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
}

用于选择目录然后删除其中所有文件的SQL查询- c#

首先我会关闭你的连接字符串后,你填写你的数据集。我会在你的DataTable上快速查看确保所有的数据都在那里。