[asp.net]刪除檔案

  • 6029
  • 0

摘要:[asp.net]刪除檔案

@程式碼:先把檔案刪除才能刪除資料夾

Public Shared Sub ClearDirectory(ByVal strPath As String, ByVal pattern As String)
        Dim strManyPaths() As String = System.IO.Directory.GetDirectories(strPath, pattern)
        '刪除該使用者所有的資料夾以及相關檔案
        For Each strSubPath As String In strManyPaths
            Dim strFileDirEnteries() As String = System.IO.Directory.GetFileSystemEntries(strSubPath)
            '先刪除資料夾裡面的檔案
            For Each strEntryFileName As String In strFileDirEnteries
                System.IO.File.Delete(strEntryFileName)
            Next
            '刪除資料夾才刪的掉
            My.Computer.FileSystem.DeleteDirectory(strSubPath, FileIO.DeleteDirectoryOption.DeleteAllContents)


        Next
    End Sub

@記得檔案的屬性不可以是唯讀喔,不然會出現拒絕存取路徑

@也必須設定web.config(有的網站會有驗證權限),所以必須將該資料夾的驗證拿掉

<location path="Temp">
        <system.web>
            <authorization>
                <allow users="*"></allow>
            </authorization>
        </system.web>
    </location>

@最後給予該資料夾的NETWORK_SERVICE適當的全線即可