13

我想在 .NET 中使用 NTFS 压缩来压缩文件夹。我找到了这篇文章,但它不起作用。它引发异常(“无效参数”)。

DirectoryInfo directoryInfo = new DirectoryInfo( destinationDir );
if( ( directoryInfo.Attributes & FileAttributes.Compressed ) != FileAttributes.Compressed )
{
   string objPath = "Win32_Directory.Name=" + "\"" + destinationDir + "\"";
   using( ManagementObject dir = new ManagementObject( objPath ) )
   {
      ManagementBaseObject outParams = dir.InvokeMethod( "Compress", null, null );
      uint ret = (uint)( outParams.Properties["ReturnValue"].Value );
   }
}

有人知道如何在文件夹上启用 NTFS 压缩吗?

4

6 回答 6

15

根据我的经验,使用 P/Invoke 通常比 WMI 更容易。我相信以下应该有效:

private const int FSCTL_SET_COMPRESSION = 0x9C040;
private const short COMPRESSION_FORMAT_DEFAULT = 1;

[DllImport("kernel32.dll", SetLastError = true)]
private static extern int DeviceIoControl(
    SafeFileHandle hDevice,
    int dwIoControlCode,
    ref short lpInBuffer,
    int nInBufferSize,
    IntPtr lpOutBuffer,
    int nOutBufferSize,
    ref int lpBytesReturned,
    IntPtr lpOverlapped);

public static bool EnableCompression(SafeFileHandle handle)
{
    int lpBytesReturned = 0;
    short lpInBuffer = COMPRESSION_FORMAT_DEFAULT;

    return DeviceIoControl(handle, FSCTL_SET_COMPRESSION,
        ref lpInBuffer, sizeof(short), IntPtr.Zero, 0,
        ref lpBytesReturned, IntPtr.Zero) != 0;
}

由于您尝试在目录上设置它,您可能需要使用 P/Invoke 调用CreateFileFILE_FLAG_BACKUP_SEMANTICS获取目录上的 SafeFileHandle。

另外,请注意,在 NTFS 中对目录设置压缩不会压缩所有内容,它只会使新文件显示为压缩(加密也是如此)。如果要压缩整个目录,则需要遍历整个目录并在每个文件/文件夹上调用 DeviceIoControl。

于 2009-03-08T22:28:49.853 回答
12

我已经测试了代码和它 替代文字

  • 确保它适用于您的 gui。也许分配单元的大小对于压缩来说太大了。或者你没有足够的权限。
  • 对于您的目的地,使用如下格式:“c:/temp/testcomp”,带有正斜杠。

完整代码:

using System.IO;
using System.Management;

class Program
{
    static void Main(string[] args)
    {
        string destinationDir = "c:/temp/testcomp";
        DirectoryInfo directoryInfo = new DirectoryInfo(destinationDir);
        if ((directoryInfo.Attributes & FileAttributes.Compressed) != FileAttributes.Compressed)
        {
            string objPath = "Win32_Directory.Name=" + "\"" + destinationDir + "\"";
            using (ManagementObject dir = new ManagementObject(objPath))
            {
                ManagementBaseObject outParams = dir.InvokeMethod("Compress", null, null);
                uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
            }
        }
     }
}
于 2009-03-08T21:04:03.020 回答
1

创建 Win32_Directory.Name=... 字符串时,您需要将反斜杠加倍,例如路径 C:\Foo\Bar 将构建为:

Win32_Directory.Name="C:\\Foo\\Bar",

或使用您的示例代码:

string objPath = "Win32_Directory.Name=\"C:\\\\Foo\\\\Bar\"";

显然,该字符串被馈送到一些需要路径字符串的转义形式的进程。

于 2010-11-15T20:27:22.200 回答
1

这是对 Igal Serban 答案的轻微改编。我遇到了一个微妙的问题,Name必须采用非常特定的格式。所以我首先添加了一些Replace("\\", @"\\").TrimEnd('\\') 魔法来规范化路径,我还清理了一些代码。

var dir = new DirectoryInfo(_outputFolder);

if (!dir.Exists)
{
    dir.Create();
}

if ((dir.Attributes & FileAttributes.Compressed) == 0)
{
    try
    {
        // Enable compression for the output folder
        // (this will save a ton of disk space)

        string objPath = "Win32_Directory.Name=" + "'" + dir.FullName.Replace("\\", @"\\").TrimEnd('\\') + "'";

        using (ManagementObject obj = new ManagementObject(objPath))
        {
            using (obj.InvokeMethod("Compress", null, null))
            {
                // I don't really care about the return value, 
                // if we enabled it great but it can also be done manually
                // if really needed
            }
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Trace.WriteLine("Cannot enable compression for folder '" + dir.FullName + "': " + ex.Message, "WMI");
    }
}
于 2013-09-27T17:13:46.187 回答
1

有一种更简单的方法,我在 Windows 8 64 位中使用,为 VB.NET 重写。享受。

    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

对我很有用。Chagne .\root 到 \pcname\root 如果您需要在另一台计算机上执行此操作。小心使用。

于 2013-10-27T17:06:52.823 回答
0

我不相信有一种方法可以在 .NET 框架中设置文件夹压缩,因为文档(备注部分)声称它不能通过File.SetAttributes完成。这似乎只在使用DeviceIoControl函数的 Win32 API 中可用。人们仍然可以通过 .NET 使用PInvoke来做到这一点。

一旦熟悉了 PInvoke,请查看pinvoke.net上的参考资料,该参考资料讨论了签名需要是什么样子才能实现这一点。

于 2009-03-08T20:48:48.627 回答