How to make a "Read only" file?

modest and cute girl picture modest and cute girl · Aug 2, 2012 · Viewed 15.5k times · Source

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();
    }

Answer

Aghilas Yakoub picture Aghilas Yakoub · Aug 2, 2012

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);

......