Solved

Change file attribute with c# cmd command

Posted on 2011-02-10
7
1,337 Views
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

0
Comment
Question by:fancyfanta
  • 3
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
jasonduan earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:fancyfanta
Comment Utility
Thank you @jasonduan,
and then how can I lock down the file? I mean I want to get back the ex attributes.
Thanks
0
 
LVL 11

Assisted Solution

by:jasonduan
jasonduan earned 500 total points
Comment Utility
you can save the attributes:
FileAttributes orgAttributes = f.Attributes;
then restore it when you done:
f.Attributes = orgAttributes;
or
File.SetAttributes(@"c:\....", orgAttributes);
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:fancyfanta
Comment Utility
It gives error:
The name 'orgAttributes' does not exist in the current context
0
 

Author Comment

by:fancyfanta
Comment Utility
Sorry my mistake.
But I want to change attributes to readonly, system file and hidden
How can i?
I dont want to orginals
0
 
LVL 15

Expert Comment

by:Russell_Venable
Comment Utility
This is one of the best examples on the net that I
You can refer too. http://www.csharp-examples.net/file-attributes/
0
 
LVL 11

Assisted Solution

by:jasonduan
jasonduan earned 500 total points
Comment Utility
this code changes attributes to readonly, system file and hidden

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now