Change file attribute with c# cmd command

Posted on 2011-02-10
Last Modified: 2013-12-17
I want to change file attribute with cmd.exe via c#.
But when I Executed the program. It don't any changes.

Please help, where's my mistake?

string arguments = @"attrib -r -h -s C:\WINDOWS\system32\drivers\etc\hosts";
System.Diagnostics.Process.Start("cmd.exe", arguments);

Open in new window

Question by:fancyfanta
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 11

Accepted Solution

jasonduan earned 500 total points
ID: 34864994
Do you really have to use "cmd.exe"?

the following C# code will have the same effect without launching a seperate process.
FileInfo f =  new FileInfo(@"C:\WINDOWS\system32\drivers\etc\hosts");
f.Attributes = f.Attributes & ~FileAttributes.ReadOnly & ~FileAttributes.System & ~FileAttributes.Hidden;

File.SetAttributes(...) can also be used to set file attributes if you know the exact desired attributes.

Author Comment

ID: 34865062
Thank you @jasonduan,
and then how can I lock down the file? I mean I want to get back the ex attributes.
LVL 11

Assisted Solution

jasonduan earned 500 total points
ID: 34865279
you can save the attributes:
FileAttributes orgAttributes = f.Attributes;
then restore it when you done:
f.Attributes = orgAttributes;
File.SetAttributes(@"c:\....", orgAttributes);
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.


Author Comment

ID: 34865357
It gives error:
The name 'orgAttributes' does not exist in the current context

Author Comment

ID: 34865546
Sorry my mistake.
But I want to change attributes to readonly, system file and hidden
How can i?
I dont want to orginals
LVL 15

Expert Comment

ID: 34870665
This is one of the best examples on the net that I
You can refer too.
LVL 11

Assisted Solution

jasonduan earned 500 total points
ID: 34871162
this code changes attributes to readonly, system file and hidden

File.SetAttributes(@"c:\....", FileAttributes.ReadOnly | FileAttributes.System | FileAttributes.Hidden);

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question