How programmatically set file attribute to not be read-only?

R

rcook349

From within my project, I am opening an file elsewhere on the network and
changing contents within.

When I go to close/save, however, I can't because it's read-only (as it's
checked in).

Can I just flip read-only off, make my changes and save it, and then flip
read only back on (programmatically)? How can I do this, please?

Thanks.
 
A

Arto Viitanen

rcook349 kirjoitti:
From within my project, I am opening an file elsewhere on the network
and changing contents within.

When I go to close/save, however, I can't because it's read-only (as
it's checked in).

Can I just flip read-only off, make my changes and save it, and then
flip read only back on (programmatically)? How can I do this, please?

Thanks.

using System.IO;

if (File.Exists(fileName))
{
FileAttributes attrs = File.GetAttributes(fileName);
File.SetAttributes(fileName, attrs & ~FileAttributes.ReadOnly);
MakeTheChanges(fileName);
attrs = File.GetAttributes(fileName);
File.SetAttributes(fileName, attrs | FileAttributes.ReadOnly);
}

There is two calls to GetAttributes, since most likely your
MakeTheChanges sets at least the archive bit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top