如题,我上传或更新图片的代码如下,应该怎样在更新另一张图片前,先将biaozhi文件夹中的图片删除?Sub Sure_Click(ByVal Sender As Object,ByVal E As EventArgs)
  
   Dim Sql As String
Dim userid As String=Request.Cookies("gongsi_id").value
Dim PathStr As String
Dim Cnn As OleDbConnection
  Dim Cmd As OleDbCommand
  Dim StrCnn As String
  If gs_logo.HasFile Then
    Try
      PathStr = Server.MapPath("job_523_ok.mdb")
      PathStr = Left(PathStr, InStrRev(Trim(PathStr), "\") - 1)
      PathStr = PathStr & "\biaozhi\" & gs_logo.FileName
      gs_logo.SaveAs(PathStr) 
    Catch ex As Exception
Exit Sub
    End Try
StrCnn = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_cnnjobok")  
    Cnn = New OleDbConnection(StrCnn)
    Cnn.Open()
Sql = "update gongsi_yh set gs_logo='" & trim(gs_logo.FileName) & "' where gongsi_id=" & trim(userid)

    Cmd = New OleDbCommand(Sql, Cnn)
    Cmd.ExecuteNonQuery()
    Cnn.Close()
end if
    end sub先谢谢大家!

解决方案 »

  1.   

     Dim Sql As String 
    Dim userid As String=Request.Cookies("gongsi_id").value 
    Dim PathStr As String 
    Dim Cnn As OleDbConnection 
      Dim Cmd As OleDbCommand 
      Dim StrCnn As String 
      If gs_logo.HasFile Then 
        Try 
          PathStr = Server.MapPath("job_523_ok.mdb") 
          PathStr = Left(PathStr, InStrRev(Trim(PathStr), "\") - 1) 
          PathStr = PathStr & "\biaozhi\" & gs_logo.FileName 
          gs_logo.SaveAs(PathStr) 
        Catch ex As Exception 
    Exit Sub 
        End Try 
    StrCnn = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_cnnjobok")  
        Cnn = New OleDbConnection(StrCnn) 
        Cnn.Open() 
    Sql = "update gongsi_yh set gs_logo='" & trim(gs_logo.FileName) & "' where gongsi_id=" & trim(userid)     Cmd = New OleDbCommand(Sql, Cnn) 
        Cmd.ExecuteNonQuery() 
        Cnn.Close() 
    '==放在这里即可==========
    System.IO.File.Delete("文件名") 
    '======================
    end if 
        end sub 
      

  2.   

    难道SaveAs执行之后不会替换同名文件吗?
      

  3.   

    贴错了,改一下:
    Dim Sql As String 
    Dim userid As String=Request.Cookies("gongsi_id").value 
    Dim PathStr As String 
    Dim Cnn As OleDbConnection 
      Dim Cmd As OleDbCommand 
      Dim StrCnn As String 
      If gs_logo.HasFile Then 
        Try 
          PathStr = Server.MapPath("job_523_ok.mdb") 
          PathStr = Left(PathStr, InStrRev(Trim(PathStr), "\") - 1) 
          PathStr = PathStr & "\biaozhi\" & gs_logo.FileName '==放在这里即可========== 
    System.IO.File.Delete("文件名") 
    '======================       gs_logo.SaveAs(PathStr) 
        Catch ex As Exception 
    Exit Sub 
        End Try 
    StrCnn = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_cnnjobok")  
        Cnn = New OleDbConnection(StrCnn) 
        Cnn.Open() 
    Sql = "update gongsi_yh set gs_logo='" & trim(gs_logo.FileName) & "' where gongsi_id=" & trim(userid)     Cmd = New OleDbCommand(Sql, Cnn) 
        Cmd.ExecuteNonQuery() 
        Cnn.Close() end if 
        end sub 
      

  4.   

    C#FileInfo fi = new FileInfo("test.htm");
                if (fi.Exists)
                {
                    fi.Delete();            }