I'm using the C# StreamWritier
class. Questions:
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();
}
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);
......
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#:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With