无法从C:\中删除文件

时间:2015-09-06 13:09:20

标签: vb.net

这段代码出了什么问题? 我写了这个程序删除一些文件而不将其发送到回收站 每次我运行程序并尝试删除(C)Root中的文件 代码菜单中显示的错误告诉我该文件无法删除

Public Class Form1
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
    Try
        Dim x As String
        x = txtfolder.Text
        If txtfolder.Text = "" Then
            MessageBox.Show("Write An Address Then Click ^Goto^", _
            "Super Deleter", MessageBoxButtons.OK, _
            MessageBoxIcon.Information)

        ElseIf MessageBox.Show("Are you sure You want to Delete File(s)?", _
                "Super Deleter", MessageBoxButtons.YesNo, _
                MessageBoxIcon.Warning) _
                = DialogResult.Yes Then
            My.Computer.FileSystem.DeleteFile(lstbox.SelectedItem)
            MessageBox.Show("File Has been Deleted Successfuly!", _
            "Siper Deleter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            lstbox.Items.Clear()
            For Each folder As String In _
            My.Computer.FileSystem.GetFiles(x)
                lstbox.Items.Add(folder)
            Next
        End If
    Catch ex As Exception
        MessageBox.Show("Can't Delete This File", "Error", _
        MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
Private Sub gotobtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gotobtn.Click
    Try
        Dim x As String
        x = txtfolder.Text
        lstbox.Items.Clear()
        For Each folder As String In _
        My.Computer.FileSystem.GetFiles(x)
            lstbox.Items.Add(folder)
        Next
    Catch ex As Exception
        MessageBox.Show("Write An Address Then Click ^Goto^", _
        "Super Deleter", MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
    End Try
End Sub

End Class

1 个答案:

答案 0 :(得分:0)

1)将lstbox.SelectedItem替换为lstbox.SelectedItem.tostring

2)您必须使用if system.io.file.exists(lstbox.SelectedItem.toString) then : DO SOMETHING : end if

检查文件是否已存在

3)使用System.IO.File.Delete ("YOUR FILE")代替My.Computer.FileSystem.DeleteFile

抱歉不好的语言= p