Making a folder compressed

  • Thread starter Thread starter Pavel
  • Start date Start date
P

Pavel

Hello.

I am trying to make a folder compressed and failing miserably. Below
are three ways that I tried to make it compressed, all of them compile
and run w/o any problems, but the folder is still not-compressed.
(using NTFS, win2k)

// some folder
string path = @"C:\TEMP\";

// try #1
File.SetAttributes(path, FileAttributes.Compressed);

// try #2
File.SetAttributes(path, File.GetAttributes(path) |
FileAttributes.Compressed);

// try #3
DirectoryInfo di = new DirectoryInfo(path);
di.Attributes = di.Attributes | FileAttributes.Compressed;

What am I doing wrong?
(I did not work before with the FlagsAttribute enums, so I could be
doing something completely wrong, but I tried to find how to do it, and
those seem to be the proper ways)

Thanks,

Pavel
 
Pavel said:
Hello.

I am trying to make a folder compressed and failing miserably. Below
are three ways that I tried to make it compressed, all of them compile
and run w/o any problems, but the folder is still not-compressed.
(using NTFS, win2k)

// some folder
string path = @"C:\TEMP\";

// try #1
File.SetAttributes(path, FileAttributes.Compressed);

// try #2
File.SetAttributes(path, File.GetAttributes(path) |
FileAttributes.Compressed);

// try #3
DirectoryInfo di = new DirectoryInfo(path);
di.Attributes = di.Attributes | FileAttributes.Compressed;

What am I doing wrong?

Nothing, you can't set this attribute using Directory.Attributes you can
only get the attribute.
The documentation should indicate which attributes can be set and which
cannot but fails to do so.
Here are those that can't be set.
Directory, Encrypted, Compressed, ReparsePoint, SparseFile, OffLine and
Temporary.
All others can be set.

You can use the System.Management and the WMI class win32_directory to
compress a folder.

string dirName = "c:\\\\someFolder";
string objPath = "Win32_Directory.Name=" + "\"" + dirName + "\"";
using (ManagementObject dir= new ManagementObject(objPath))
{
ManagementBaseObject outParams = dir.InvokeMethod("Compress", null,
null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
Console.WriteLine("Success");
else Console.WriteLine("Failed with error code: {0}", ret);
}

Willy.
 
Thanx for your reply Willy. The code worked for me, I am currently
running a test to see whether there is a benefit (for my program) to
compress the folder.

pavel
 
Well, you can always un-compress :-) using the uncompress command:

ManagementBaseObject outParams = dir.InvokeMethod("Uncompress", null,
null);

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top