问大家这样一个问题:
我上传图片的时候,是把图片保存在一个文件夹下的,把它的路径存在数据库中的,现在我要做后台,在删除该图片的路径的同时也要删除文件夹下相对应的图片.求源代码.

解决方案 »

  1.   

    System.IO.File.Delete(文件路径字符串) 我在弄 ,可是出错,说正在使用图片,不能删除。。 郁闷啊。
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;namespace MapDeleteAndSql
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class main : System.Windows.Forms.Form
    {
    private static int i = 1;
    private static int g = 1;
    private static string str;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public main()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // pictureBox1
    // 
    this.pictureBox1.Location = new System.Drawing.Point(16, 16);
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(280, 184);
    this.pictureBox1.TabIndex = 0;
    this.pictureBox1.TabStop = false;
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(16, 224);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(64, 32);
    this.button1.TabIndex = 1;
    this.button1.Text = "上一张";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(120, 224);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(64, 32);
    this.button2.TabIndex = 2;
    this.button2.Text = "一下张";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(216, 224);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(64, 32);
    this.button3.TabIndex = 3;
    this.button3.Text = "删除";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // main
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
    this.ClientSize = new System.Drawing.Size(312, 278);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.pictureBox1);
    this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
    this.Name = "main";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "删除SQL中的图片";
    this.Load += new System.EventHandler(this.main_Load);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new main());
    }

    private void main_Load(object sender, System.EventArgs e)
    {
    SqlConnection conn = new SqlConnection("server = (local);Integrated Security = SSPI; Database = song;");
    SqlCommand comm = new SqlCommand("SELECT path FROM map",conn);
    try
    {
    conn.Open();
    SqlDataReader thisReader = comm.ExecuteReader();
    thisReader.Read();
    str = thisReader[0].ToString();
    this.pictureBox1.Image=Image.FromFile(thisReader[0].ToString());
    while ( thisReader.Read() )
    {
    g+=1;
    }
    thisReader.Close();
    //MessageBox.Show(g.ToString());
    }
    catch( SqlException ex)
    {
    MessageBox.Show(ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    }
    private void Showmap(int i)
    {
    if ( i >= 0 )
    {
    SqlConnection conn = new SqlConnection("server = (local);Integrated Security = SSPI; Database = song;");
    SqlCommand comm = new SqlCommand("SELECT path FROM map",conn);
    try
    {
    conn.Open();
    SqlDataReader thisReader = comm.ExecuteReader();
    int k = 0;
    while( thisReader.Read() )
    {
    k++;
    if ( k == i)
    {
    str = thisReader.GetString(0);
    //MessageBox.Show(str);
    this.pictureBox1.Image=Image.FromFile(str);
    break;
    }
    }
    }
    catch( SqlException ex)
    {
    MessageBox.Show(ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    }
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    if ( i> 1 )
    {
    i-=1;
    //MessageBox.Show(i.ToString());
    Showmap(i);
    }
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if ( i < g )
    {
    i+=1;
    //MessageBox.Show(i.ToString());
    Showmap(i);
    }
    }
    private void button3_Click(object sender, System.EventArgs e)
    {
    SqlConnection conn = new SqlConnection("server = (local);Integrated Security = SSPI; Database = song;");
    //MessageBox.Show(str);
    string ss = "DELETE FROM map WHERE path="+str.Trim().ToString();
    MessageBox.Show(ss.ToString());
    SqlCommand comm = new SqlCommand(ss,conn);
    try
    {
    conn.Open();
    //comm.ExecuteNonQuery();
    g--;
    }
    catch( SqlException ex)
    {
    MessageBox.Show(ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    if( g > 0 )
    {

    if ( i < g )
    {
    i+=1;
    //MessageBox.Show(i.ToString());
    Showmap(i);
    }


    }
    System.IO.File.Delete(str);
    } }
    }
    刚刚学习 .NET 学得乱七八糟 请大家多多指教