用asp.net开发一上传系统文件系统,请教当上传了一文件,比如说是.doc等文件, 我只将文件的路径存放到数据库字段(比如说字段名为FilePath)中,而此文件上传到对应的文件夹中,如果我想删除这条记录的同时将已上传的这个文件也删除,应该怎么样解决呢?有没有办法解决呢?谢谢!

解决方案 »

  1.   

    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemIOFileClassDeleteTopic.htm从数据库读取文件的路径,然后按照上面的方法删除
      

  2.   

    就是从数据库得到path2,然后用File.Delete(path2);删除贝
      

  3.   

    string conn=System.Configuration.ConfigurationSettings.AppSettings["yzc112"].ToString();
    OleDbConnection myConnection=new OleDbConnection(conn); string sqldelfile="select * from fq_email where email_jslogin1='"+Request["email_jslogin1"]+"' and email_id="+Request["email_id"];
    OleDbDataAdapter dadelfile=new OleDbDataAdapter(sqldelfile,myConnection);
    DataSet dsdelfile=new DataSet();
    dadelfile.Fill(dsdelfile,"host");
    string file=dsdelfile.Tables["host"].Rows[0][10].ToString();
    string path = @"D:\yzc112\OA系统\govoa\email\upfile1+file";
    try 
    {
    using (StreamWriter sw = File.CreateText(path)) {}
    string path2 = path + "temp"; // Ensure that the target does not exist.
    File.Delete(path2); // Copy the file.
    File.Copy(path, path2);
    Console.WriteLine("{0} was copied to {1}.", path, path2); // Delete the newly created file.
    File.Delete(path2);
    Console.WriteLine("{0} was successfully deleted.", path2);

    catch (Exception e) 
    {
    Console.WriteLine("The process failed: {0}", e.ToString());
    }
    this.DataBind();
    通过什么触发事件来删除呢?
      

  4.   

    真接File.Delete(路径)把文件删了就OK了,最好先删掉数据库里的数据。
      

  5.   

    to:accpdingyi(未来精英) 应该是先删除文件然后,根据是否删除后。再删除数据库里的数据吧!
      

  6.   

    你的关键点是如何触发,其实有个更好的解决方案,就是用触发器来做。当删除某条数据的时候,在触发其里面得到删除这条数据的Path字段的值,然后运行MASTER.DBO.XP_CMDSHELL 'del  c:\qq\help_01.gif'c:\qq\help_01.gif  这个换为你的path
      

  7.   

    用asp.net开发一上传系统文件系统,请教当上传了一文件,比如说是.doc等文件, 我只将文件的路径存放到数据库字段(比如说字段名为FilePath)中,而此文件上传到对应的文件夹中,如果我想删除这条记录的同时将已上传的这个文件也删除,应该怎么样解决呢?
    这个问题我刚处理过,给你个思路,代码挺多的。贴出来太多
    1 客户端上传文件 http://www.cnblogs.com/jhtchina/articles/82509.html 看我的博客
    private string uploadFile()
    {
    try
    {
    string UploadFullFileName;
    string uploadFileFullName;//FilePath,FileName
                    //uploadFileFullName=btnuploadFile.Value;
    uploadFileFullName=Fileupload.Value;
    int i=uploadFileFullName.LastIndexOf("\\");
    string FileName=uploadFileFullName.Substring(i+1);
                    //string strPath=;//»ñµÃ·þÎñÆ÷¶ËµÄµ±È¨Ä¿Â¼
    //uploadFileFullName=strPath+FileName;
    string uploadFilePath=ConfigurationSettings.AppSettings["uploadFilePath"];
       

    string ClientIP=Request.UserHostAddress;//¿Í‘ôÂZücIP        
    string strIP="";
    //„hµôÆäÖеÄ.
    for (int itemp=0;itemp<ClientIP.Length;itemp++)
    {
    if (ClientIP[itemp]=='.')
    {
                            strIP+="-";
    continue;                        
    }
                        strIP+=ClientIP[itemp];
    }
                    strIP+="--";
                    //&micro;&Atilde;&micro;&frac12;&reg;”&Ccedil;°&#8226;rég
    string strGetDateTime=DateTime.Now.ToString();
                    string strDateTime="";
    for (int itemp=0;itemp<strGetDateTime.Length;itemp++)
    {
    if ((strGetDateTime[itemp]==' ')||(strGetDateTime[itemp]==':')||(strGetDateTime[itemp]=='/'))
    {
                            strDateTime+="-";
                            continue;
    }
                        strDateTime+=strGetDateTime[itemp];
    }
    string strFullFileName=strIP+strDateTime;//&#8218;€&micro;&frac12;&Eacute;&Iuml;&#8218;÷&Ograve;&Ocirc;&ordm;ó&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;&Atilde;&ucirc;·Q
                    Fileupload.PostedFile.SaveAs(uploadFilePath+strFullFileName); 
    UploadFullFileName=strFullFileName;
    return UploadFullFileName;//±&pound;&acute;&aelig;&micro;&Auml;&Ecirc;&Ccedil;&Icirc;&Auml;&frac14;&thorn;,&frac14;U&Ecirc;&frac12;&Ecirc;&Ccedil;&Agrave;M&frac12;y&Ouml;&cedil;&para;¨&micro;&Auml;&Auml;&iquest;&auml;&#8250; }
    catch
    {
    return null;
    }
    }
      

  8.   

    上面上传以后的文件名是=客户端IP+当前日期时间 
    uploadFilePath=ConfigurationSettings.AppSettings["uploadFilePath"]; 获得服务器上传文件的相对位置.
    这些做完以后,开始写数据库strUploadFullFileName=uploadFile();
     MsiProcessClass MsiPC=new MsiProcessClass((string)Session["strConn"]); int iMissionWorkID=int.Parse(savevalue2.Text);
    if (MsiPC.b_UploadMsikUpdateAppendFile(iMissionWorkID,strUploadFullFileName))
    {
    lbShowFileContent.Text=strUploadFullFileName;
    Response.Write("<script>window.alert('Update OK&pound;&iexcl;');</script>");
                                btnUploadFile.Visible=false;
                                ShowDataGrid();
    }
    else
    {
        Response.Write("<script>window.alert('Update Error&pound;&iexcl;');</script>");
    }
      

  9.   

    就是删除一个文件,用得着那么多代码吗?用Sql语句不行吗?我试过啦,删得很好啊。不过你需要考虑你的权限。我是在sa帐号测试的。可以。
      

  10.   

    数据库在这里的操作可以直接Sql insert 也可以Create Procedure ALTER  procedure L_2MsikUpdateAppendFile
    @MissionWorkID int,
    @Msik_AppendFile varchar(80),
    @returnresult int output
    as
    update L_mainTable
    set Msik_AppendFile=@Msik_AppendFile
    where MissionWorkID=@MissionWorkID
    set @returnresult=@@ERROR
    上面 Msik_AppendFile 保存的是文件名称 根据数据表里面的的一个唯一性MissionWorkID
      

  11.   

    这里的Msik_AppendFile 就是保存的上传文件名称
    同理,如果再次上传,则删除指定目录: uploadFilePath=ConfigurationSettings.AppSettings["uploadFilePath"];  
    下面的Msik_AppendFile从新上传就OK了
      

  12.   

    if(System.IO.File.Exists(Server.MapPath(filePath)))
    {
    System.IO.File.Delete(Server.MapPath(filePath));
    }
    filePath是从数据库里取出来的相对路径:如../temp/fileName也可以
    if(System.IO.File.Exists(filePath))
    {
    System.IO.File.Delete(filePath);
    }filePath是从数据库里取出来的绝对路径:如C:\Inetpub\wwwroot\EHS\temp\fileName
      

  13.   

    一定要设置存储文件的文件夹的权限,networkservice(好像是这个)权限给够