Click here to Skip to main content
15,914,074 members
Home / Discussions / C#
   

C#

 
GeneralRe: stores files for my project Pin
Luc Pattyn27-Feb-10 15:23
sitebuilderLuc Pattyn27-Feb-10 15:23 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 15:30
professionalDaveyM6927-Feb-10 15:30 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 15:39
professionalDaveyM6927-Feb-10 15:39 
GeneralRe: stores files for my project Pin
Luc Pattyn27-Feb-10 15:45
sitebuilderLuc Pattyn27-Feb-10 15:45 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 15:50
professionalDaveyM6927-Feb-10 15:50 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 15:59
professionalDaveyM6927-Feb-10 15:59 
GeneralRe: stores files for my project Pin
Luc Pattyn27-Feb-10 16:30
sitebuilderLuc Pattyn27-Feb-10 16:30 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 17:57
professionalDaveyM6927-Feb-10 17:57 
All users get read and execute permissions on folders/files created there - but not write/modify.

It can be altered of course by creating a folder for your app and setting permissions at that time.
Modified code below works so all users have Write and Modify as well so the store can be truly shared.
I'll clean up the code and put it into a reusable class and post as a Tip/Trick tomorrow.
C#
using System;
using System.IO;
using System.Security.AccessControl;

namespace ConsoleApplication
{
    class Program
    {
        public static readonly string UserPath = 
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        public static readonly string AllUsersPath = 
            Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

        static void Main(string[] args)
        {
            try
            {
                string folderName = "Test";
                string fileName = "Test.txt";
                string textToSave = "AbcDefGhi";

                string userDirectory = Path.Combine(UserPath, folderName);
                string allUsersDirectory = Path.Combine(AllUsersPath, folderName);

                string userFilePath = Path.Combine(userDirectory, fileName);
                string allUsersFilePath = Path.Combine(allUsersDirectory, fileName);

                CreateDirectoryWithPermissions(userDirectory);
                CreateDirectoryWithPermissions(allUsersDirectory);

                using (TextWriter userTextWriter = new StreamWriter(userFilePath),
                allUsersTextWriter = new StreamWriter(allUsersFilePath))
                {
                    Console.WriteLine("Writing {0} to {1}", textToSave, userFilePath);
                    userTextWriter.WriteLine(textToSave);
                    Console.WriteLine("Writing {0} to {1}", textToSave, allUsersFilePath);
                    allUsersTextWriter.WriteLine(textToSave);
                }

                using (TextReader userTextReader = new StreamReader(userFilePath),
                allUsersTextReader = new StreamReader(allUsersFilePath))
                {
                    Console.WriteLine("Reading from {0}", userFilePath);
                    Console.WriteLine(userTextReader.ReadLine());
                    Console.WriteLine("Reading from {0}", allUsersFilePath);
                    Console.WriteLine(allUsersTextReader.ReadLine());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }

        private static void CreateDirectoryWithPermissions(string path)
        {
            if (!Directory.Exists(path))
            {
                bool modified = false;
                DirectoryInfo directoryInfo = directoryInfo = Directory.CreateDirectory(path);
                DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
                AccessRule rule = new FileSystemAccessRule(
                    "Users",
                    FileSystemRights.Write |
                    FileSystemRights.ReadAndExecute |
                    FileSystemRights.Modify,
                    InheritanceFlags.ContainerInherit |
                    InheritanceFlags.ObjectInherit,
                    PropagationFlags.InheritOnly,
                    AccessControlType.Allow);
                directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
                directoryInfo.SetAccessControl(directorySecurity);
            }
        }
    }
}
Dave

Tip: Passing values between objects using events (C#)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

GeneralRe: stores files for my project Pin
DaveyM6928-Feb-10 6:47
professionalDaveyM6928-Feb-10 6:47 
GeneralRe: stores files for my project Pin
Luc Pattyn27-Feb-10 15:46
sitebuilderLuc Pattyn27-Feb-10 15:46 
GeneralRe: stores files for my project Pin
DaveyM6927-Feb-10 15:52
professionalDaveyM6927-Feb-10 15:52 
AnswerRe: stores files for my project Pin
Not Active28-Feb-10 1:54
mentorNot Active28-Feb-10 1:54 
AnswerRe: stores files for my project Pin
DaveyM6928-Feb-10 6:46
professionalDaveyM6928-Feb-10 6:46 
Questionversion control software Pin
Khan Olid Mannan27-Feb-10 8:38
Khan Olid Mannan27-Feb-10 8:38 
AnswerRe: version control software Pin
Kristian Sixhøj27-Feb-10 8:40
Kristian Sixhøj27-Feb-10 8:40 
AnswerRe: version control software Pin
Som Shekhar27-Feb-10 9:34
Som Shekhar27-Feb-10 9:34 
GeneralRe: version control software Pin
Khan Olid Mannan27-Feb-10 15:16
Khan Olid Mannan27-Feb-10 15:16 
GeneralRe: version control software Pin
Som Shekhar27-Feb-10 17:50
Som Shekhar27-Feb-10 17:50 
GeneralRe: version control software Pin
Khan Olid Mannan28-Feb-10 0:45
Khan Olid Mannan28-Feb-10 0:45 
GeneralRe: version control software Pin
Som Shekhar28-Feb-10 0:50
Som Shekhar28-Feb-10 0:50 
AnswerRe: version control software Pin
Wes Aday27-Feb-10 16:04
professionalWes Aday27-Feb-10 16:04 
GeneralRe: version control software Pin
Khan Olid Mannan28-Feb-10 0:53
Khan Olid Mannan28-Feb-10 0:53 
GeneralRe: version control software Pin
Wes Aday28-Feb-10 5:15
professionalWes Aday28-Feb-10 5:15 
AnswerRe: version control software Pin
Scott Serl27-Feb-10 16:25
Scott Serl27-Feb-10 16:25 
AnswerRe: version control software Pin
David Skelly28-Feb-10 22:40
David Skelly28-Feb-10 22:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.