11

I'm using the C# StreamWritier class. Questions:

  1. How can I make a file read-only, so that no one can delete or write to it?
  2. How can I make a hidden file?

I'm creating the file like so:

    private void button1_Click(object sender, EventArgs e)
    {
        SaveFileDialog save = new SaveFileDialog();
        save.FileName = textBox1.Text;
        save.Filter = "Text File | *.rtf";


        if (save.ShowDialog() == DialogResult.OK)
        {
            StreamWriter writer = new StreamWriter(save.OpenFile());
            writer.WriteLine(textBox2.Text);
        }

        writer.Dispose();
        writer.Close();
    }
Alexander R
  • 2,468
  • 24
  • 28
modest and cute girl
  • 785
  • 4
  • 13
  • 24
  • You can look at that post: http://stackoverflow.com/a/5398398/1252575 Here's another one: http://stackoverflow.com/questions/7590446/set-file-permissions-in-c-sharp – Nickon Aug 02 '12 at 13:08
  • @peer i dont accept ur editing cuz u modified my code and deleted one of my question. – modest and cute girl Aug 02 '12 at 13:14
  • @Nickon the user peer edited my question it has a second part also. – modest and cute girl Aug 02 '12 at 13:16
  • Ahh, ok;) didn't know that. If u want normally hide ur file, use this: http://stackoverflow.com/questions/1199571/how-to-hide-file-in-c But when u want make it invisible in your file system, try PInvoke: http://www.pinvoke.net/default.aspx/kernel32.setfileattributes http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/0469eb4f-9ee0-425f-ae90-382f2902898f By invisible in your file system I understand hiding like in SystemVolumeInformation folder – Nickon Aug 02 '12 at 13:51

5 Answers5

14

Hello you can try with this method

1

 public static void SetFileReadAccess(string FileName, bool SetReadOnly)
 {
      FileInfo fInfo = new FileInfo(FileName);

      // Set the IsReadOnly property.
      fInfo.IsReadOnly = SetReadOnly;

 }

2

File.SetAttributes(yourFilePath, FileAttributes.Hidden);

......

modest and cute girl
  • 785
  • 4
  • 13
  • 24
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
11

You can set the ReadOnly attribute using File.SetAttributes.

Example:

File.SetAttributes(textBox1.Text, FileAttributes.ReadOnly);

Note that this only sets the readonly flag, it does not modify the NTFS access control lists (meaning that every skilled user can remove the read-only attribute). Note also that this resets all other attributes of the file, which should not be a problem in your case, since your are creating a new file anyway. If you need to keep existing attributes, use File.GetAttributes first and combine the existing flags with your new one (see the example on the linked MSDN page).


If you need to secure the file against malicious write attemts, you must understand NTFS security (google for "NTFS security" for lots of resources). Once you understand that, the following question will tell you how to modify them in C#:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
6

Use this for a Read Only file:

FileAttributes yourFile = File.GetAttributes(yourFilePath);
File.SetAttributes(yourFilePath, FileAttributes.ReadOnly);

Where "yourFilePath" is a string.

For a hidden file:

FileAttributes yourFile = File.GetAttributes(yourFilePath);
File.SetAttributes(yourFilePath, FileAttributes.Hidden);

And for a normal file (not Read Only, nor Hidden):

FileAttributes yourFile = File.GetAttributes(yourFilePath);
File.SetAttributes(yourFilePath, FileAttributes.Normal);

I know you didn't ask for setting a normal file but I think it's useful to know this.

Rodrigo Guedes
  • 1,169
  • 2
  • 13
  • 27
  • Rodrio can you check the second part of my question please someone edited and deleted the second part so i re-edited. thanks. by the way i voted you all up. – modest and cute girl Aug 02 '12 at 13:20
  • thanks a lot i think all your answers works and i voted you all up but i already accepted Aghilas Yakoub answer so don't misunderstand me. i really thank you all. – modest and cute girl Aug 02 '12 at 13:57
2

Same answer, but with one line of code:

// Hide and read-only in one line
File.SetAttributes(filePathFinal, FileAttributes.ReadOnly | FileAttributes.Hidden);
Mr. B
  • 2,845
  • 1
  • 21
  • 31
0

One-line answer to question #1 (without changing other file attributes):

new FileInfo(filename).IsReadOnly = true;
Udi Y
  • 258
  • 3
  • 12