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
  • 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);
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem!!! 8 33
C# LINQ ForEach() question 6 28
Release Dynamically Allocated Memory in C# 3 28
SQL Query Help Top 1 and Distinct? 6 26
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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