Click here to Skip to main content
15,898,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to write text file and want to set the attributes of that text file to "System" , "ReadOnly" , "Archive" , "Hide".

As you can do by right clicking on the file and check/uncheck the attributes of file.

How I can do this using C#.
Posted
Updated 2-Feb-12 2:57am
v2
Comments
OriginalGriff 2-Feb-12 8:49am    
Sorry? What are you trying to achieve? Remember that we can't see your screen, or read you HDD or your mind...
Use the "Improve question" widget to edit your question and provide better information.
ZurdoDev 2-Feb-12 8:54am    
What does hide text file mean?

Use the FileAttribute for this
File.SetAttributes("pathToFile",FileAttributes.Hidden)

see the below links
http://www.csharp-examples.net/file-attributes/[^]
Thanks
--RA
 
Share this answer
 
These manipulations are totally naive. You can hide the file or directory, any other code can show it. It does not provide any protection at all. In fact nearly all File Managers routinely provide you with the option to see all the files. Even read-only files are, say, deleted, after just one warning. In this way, manipulations with attributes simply make no sense. You can consider this part of file system functionality "almost" obsolete.

You can do nothing about this unless you use permissions. You can start from here:
http://msdn.microsoft.com/en-us/library/aa720406%28v=vs.71%29.aspx[^].

See also this discussion: http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/c513ca26-9bf8-4e39-a993-4ebf90aaece6/[^].

However, I would advice not doing it. Messing up with permission and hide anything, taking the risk to leave hidden files forever due to different mistakes? Looks childish. Consider 1) doing everything on an open way, which might be your best choice; 2) using encryption (which is much stronger and more robust then any system tricks taken together, 3) using resources embedded in an executable; 4) using isolated storage if applicable.

On isolated storage, please see:
http://msdn.microsoft.com/en-us/library/3ak841sy.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx[^],
http://msdn.microsoft.com/en-us/library/cc221360%28v=vs.95%29.aspx[^].

See also this CodeProject article: Isolated Storage in .NET to store application data[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900