C# 逐个删除多个文件

C# 逐个删除多个文件,c#,C#,我正试图以这种方式从目录中一个接一个地删除两个文件: protected void BtnDelete_Click(object sender, EventArgs e) { string pdfUrl = string.Empty; string imgUrl = string.Empty; SqlCommand sCmd = new SqlCommand(string.Format("SELECT PDFUrl, ImgUrl FROM Book WHERE Id=

我正试图以这种方式从目录中一个接一个地删除两个文件:

 protected void BtnDelete_Click(object sender, EventArgs e)
{
    string pdfUrl = string.Empty;
    string imgUrl = string.Empty;
    SqlCommand sCmd = new SqlCommand(string.Format("SELECT PDFUrl, ImgUrl FROM Book WHERE Id='{0}'", TextBoxDelete.Text.Trim()), con);
    con.Open();
    Object result = sCmd.ExecuteScalar();
    con.Close();
    if (result != DBNull.Value)
    {
        pdfUrl = result.ToString();
        imgUrl = result.ToString();

        string fullPathPDF = Server.MapPath(pdfUrl);
        string fullPathImg = Server.MapPath(imgUrl);

        if (File.Exists(fullPathPDF))
        {
            File.Delete(fullPathPDF);
        }

        if (File.Exists(fullPathImg))
        {
            File.Delete(fullPathImg);
        }
    }

    SqlCommand cmd = new SqlCommand("DELETE FROM Book WHERE Id='" + TextBoxDelete.Text.ToString() + "'", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}
但只有第一个文件被删除。如何删除这两个文件?一个接一个,因为在这种情况下,只有PDFUrl被删除。

无法返回两个结果。 它返回第一行的第一列,因此imgUrl字符串与pdfUrl字符串相同

您应该更改代码以使用SqlDataReader

protected void BtnDelete_Click(object sender, EventArgs e)
{
    string pdfUrl = string.Empty;
    string imgUrl = string.Empty;

    // Using a parameterized query to avoid malicious user text that could wreak havoc....
    // Sql Injection is a serious problem. Always use a parameterized query......
    string cmdText = @"SELECT PDFUrl, ImgUrl FROM Book WHERE Id=@id";

    // Enclose disposable objects in a using statement 
    // DO NOT KEEP a global connection object... there is the connection pool
    using(SqlConnection con = new SqlConnection(.....connectionstringhere....))
    using(SqlCommand cmd = new SqlCommand(cmdText, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@id", TextBoxDelete.Text.Trim());

        // ask the command to create a reader for us....
        using(SqlDataReader reader = cmd.ExecuteReader())
        {
            // Check if a record is returned by the reader
            if(reader.Read())
            {
               // OK we could get it but not if it is DBNull.....
               pdfUrl = reader.IsDbNull(0) ? string.Empty : reader[0].ToString();
               imgUrl = reader.IsDbNull(1) ? string.Empty : reader[1].ToString();

               // Prepare the full path for the two files...
               string fullPathPDF = pdfUrl.Length > 0 ? Server.MapPath(pdfUrl) : string.Empty;
               string fullPathImg = imgUrl.Length > 0 ? Server.MapPath(imgUrl) : string.Empty;

               // Start deleting them
               if (File.Exists(fullPathPDF))
                   File.Delete(fullPathPDF);
               if (File.Exists(fullPathImg))
                   File.Delete(fullPathImg);

               // CLOSE THE READER BEFORE EXECUTING ANOTHER COMMAND
               // This could be removed if your connection string uses 
               // MultipleActiveResultSets=True   (MARS)
               reader.Close();

               // The parameter is still there, the command is still linked to the connection
               // Just change the commandtext and execute....
               cmd.CommandText = "DELETE FROM Book WHERE Id=@id";
               cmd.ExecuteNonQuery();
            }
        }
    }
}

实际上,您正试图一个接一个地删除两个文件。答案取决于你得到的结果。如果未引发异常,则检查时该文件不存在。如果抛出异常,那么,向我们展示异常。就像Rotem所说的,没有什么是同时简单地一个接一个地删除的,并且从代码中,如果您有两个路径,那么都应该删除。仅供参考,在删除之前,您不必检查文件是否存在。如果它不存在,什么也不会发生。@Lucastrezesniewski你让我大吃一惊@Lucastzesniewski如果路径是空字符串或仅仅是一条路径,那就不是了……史蒂夫,很好的解释!谢谢…是的,它起作用了: