Martin Doms
Martin Doms

Reputation: 8748

How can I create a file that can be accessed by any user?

My software needs to generate files on the filesystem that can be read by any other instance of my program, regardless of the user account it's running under. The problem I'm having is that if a file is created under an admin account then I run into an UnauthorizedAccessException when trying to read the contents of the file under a different account. Here's the code I'm using to create the file

using (var fileStream = File.Create(path))
using (var streamWriter = new StreamWriter(fileStream))
{
    streamWriter.Write(/*some data*/);
}

and to read from the file

using (var fileStream = new FileStream(fileName, FileMode.Open))
using (var streamReader = new StreamReader(fileStream))
{
    var idLine = streamReader.ReadLine();
    if (idLine != null) fileContents = idLine;
}

Thanks

Upvotes: 0

Views: 445

Answers (2)

Yahia
Yahia

Reputation: 70369

Call Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) to get an application specific path which is accessible (read/write etc.) by all users/accounts on that computer.

Upvotes: 0

Francis
Francis

Reputation: 3383

If you want to change the access to your file, you have to use the AccessControl : http://msdn.microsoft.com/en-us/library/9c13ttb3.aspx

Upvotes: 2

Related Questions