Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Change file attribute with c# cmd command

Posted on 2011-02-10
Medium Priority
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 2000 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 2000 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);
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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. http://www.csharp-examples.net/file-attributes/
LVL 11

Assisted Solution

jasonduan earned 2000 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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

730 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