Compress a folder using NTFS compression in .NET

前端 未结 6 1237
Happy的楠姐
Happy的楠姐 2021-02-01 09:48

I want to compress a folder using NTFS compression in .NET. I found this post, but it does not work. It throws an exception (\"Invalid Parameter\").

DirectoryI         


        
6条回答
  •  花落未央
    2021-02-01 09:54

    There is a much simpler way, which I am using in Windows 8 64-bit, rewritten for VB.NET. Enjoy.

        Dim Path as string = "c:\test"
        Dim strComputer As String = "."
        Dim objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Dim colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where name = '" & Replace(path, "\", "\\") & "'")
        For Each objFolder In colFolders
            objFolder.Compress()
        Next
    

    works great for me. Chagne .\root to \pcname\root if you need to do it on another computer. Use with care.

提交回复
热议问题