473,395 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Making a folder compressed

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

Nov 16 '05 #1
4 14060

"Pavel" <ea**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.
Nov 16 '05 #2
I believe you cannot set a file attribute to be compressed, it's a property
that is readonly. For example, a text file would not be compressed, where as
a Zip file would be.

If you want to compress code, then you have to manually zip it up, then
remove the folder.

See this: http://www.icsharpcode.net/OpenSourc...b/Default.aspx

Good luck.
Dan.

"Pavel" <ea**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 16 '05 #3
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

Nov 16 '05 #4
Well, you can always un-compress :-) using the uncompress command:

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

Willy.

"Pavel" <ea**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Roland Hall | last post by:
It appears what I'm reading is that Windows 2K3, like XP, supports what MSFT calls a compressed zipped folder. Is this exposed so I can effectively use FSO or a shell to create one of these and...
0
by: Vladimir Nesterovsky | last post by:
Hello, I'm having a trouble with a Shell.Folder object when I'm trying to use it from a C++ application, but at the same time, the same object works fine from within html script. The effect...
3
by: Justin Busch | last post by:
I thought I saw an article online about programmatically using the Send To Compressed (zipped) Folder function in VB, however that page no longer exists. Does anyone have details on how this is...
2
by: CK | last post by:
One way to save storage space is to put the SQL data files into a compressed file. Has anyone got any idea how this will affect the query speed?
5
by: _DG | last post by:
At least when you've got any docs in .CHM format in the folder tree. If you drop a file called CSharpDocs.chm into a folder: \toplevel\C#\docs, you can open it, but it won't display. The #...
1
by: Phil Haddock | last post by:
Can anyone post a code snippet on how to retrieve Permission information on a Folder? (Visual Basic 2005) There's lot's of information on how to set a permission, but I can't figure out how to...
2
by: mravichandran | last post by:
dear Readers, i will be downloading a zip file from the intranet website programmatically and i would like to process some of the files inside them before the user can save it. we have a lot of...
2
by: steggun | last post by:
Hello, I'm writing a C# windows form application and need some guidance on a couple of issues. First, my application needs to change the permissions on a folder. The application creates a...
1
by: =?Utf-8?B?S2V5QmVl?= | last post by:
- YOU CAN PROTECT ZIPPED COMPRESSED FOLDER WITH A PASSWORD (PW). - YOU MUST PROVIDE THE PW BEFORE THE FILE CAN BE EXTRACTED. - HOW CAN YOU EXTRACT THE FILE IF YOU FORGOT THE PW ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.