1

My application needs to write some information to a file and keep it common to the machine(common to all users).

Currently I stored it in C:\Documents and Settings\All Users\Application Data

But the problem is whenever the file is created by admin all the other users doesn't have write permission to it.

But I want write permission to the file for all users, so that all the users can modify data in it. Could any one help me in this?

EDIT: I want a store an xml file that is common to all users.

thanks

Bhaskar
  • 1,680
  • 7
  • 26
  • 40

3 Answers3

0

will the registry be a good place for you? (depends what you want to store). you can create a node on the HKLM hive to be accessed by all users

NirMH
  • 4,769
  • 3
  • 44
  • 69
0

I would suggest you study the File.SetAccessControl() method and FileSecurity class. In particular, the FileSecurity documentation provides a complete example of modifying the file access control list (permissions set) for a given file. Here is a simplified version of that example, which gives everyone read and write permission for C:\TEMP\junk.txt:

using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

class WorldWrite
{
    static void Main()
    {
        string filename = @"C:\TEMP\junk.txt";
        if (!File.Exists(filename)) {
            Console.WriteLine("No such file " + filename);
            return;
        }

        /* Get the previous ACL, add a rule to give everyone read
         * and write permission, and write back the resulting ACL. */
        FileSecurity fileACLs = File.GetAccessControl(filename);
        fileACLs.AddAccessRule(new FileSystemAccessRule(
            // For everyone 
            new SecurityIdentifier (
                WellKnownSidType.WorldSid, 
                null /* domain SID; ignored for Everyone */
            ),
            // Allow read and write (of data and metadata), 
            // but not delete or execute
            FileSystemRights.Read | FileSystemRights.Write,
            AccessControlType.Allow
        ));
        File.SetAccessControl(filename, fileACLs);
        Console.WriteLine("Success!");
    }
}

I'm also concerned about how you are specifying the shared application data directory. If you're hard-coding the name C:\Documents and Settings\All Users\Application Data, then your code won't work on non-English versions of Windows, or Vista or Windows 7 for that matter. It may be better to use the SpecialFolder enum to find the right directory to use.

Michael Ratanapintha
  • 39,422
  • 4
  • 33
  • 40
  • I have already tried this, but the problem is only the owner of the file will be able to change access of the file. So I am planning to change write access to user group 'Everyone' as soon as it is created for the first time. Anyhow thanks for your answer! – Bhaskar Jul 04 '11 at 07:24
  • Wait, so you needed someone *other* than the file owner to change its permissions?! I hope you understand why only trusted users (such as admins!) normally have that right... – Michael Ratanapintha Jul 04 '11 at 17:51
0

If you are using ClickOnce deployment, this will be difficult to do, because you have to elevate privileges in order to read/write to a common folder, and ClickOnce apps won't do that.

You could create a small exe file that does what you want, and have the clickonce application invoke that and let it elevate privileges. That's about the only option you have.

RobinDotNet
  • 11,723
  • 3
  • 30
  • 33
  • I have fixed it by granting write permission to everyone while creating the file for the first time.. Now the other users are able to modify the file :) – Bhaskar Jul 08 '11 at 04:00
  • So are you manually creating the file? Or are you doing it from the ClickOnce app, and if so, how? – RobinDotNet Jul 08 '11 at 07:59
  • But how did you grant write permission to everyone? Did you do that programmatically or manually? – RobinDotNet Jul 11 '11 at 05:07
  • In the ClickOnce app? It let you create the folder and give write permission to everyone when you created it? I get this question a lot, so it's great if that's how you got it to work, and I can recommend the same solution to others. – RobinDotNet Jul 13 '11 at 16:39
  • This would help you I think http://stackoverflow.com/questions/6533433/creating-xml-file-with-write-access-given-to-all-the-windows-account-users – Bhaskar Jul 14 '11 at 03:47