0

i am working on a project that requires a folder to be accessed by only w3wp.exe process. no other user can access this folder on the machine

i am working on a console project my implementation so far is

      public static void SetFolderPermission(string folderPath){

        bool exists = Directory.Exists(folderPath);
        if (!exists)
        {
            DirectoryInfo di = System.IO.Directory.CreateDirectory(folderPath);
            Console.WriteLine("The Folder is created Sucessfully");
        }
        else
        {
            Console.WriteLine("The Folder already exists");
        }
        var directoryInfo = new DirectoryInfo(folderPath);
        var directorySecurity = directoryInfo.GetAccessControl();
        var currentUserIdentity = GetIISProcessID("w3wp");
            //WindowsIdentity.GetCurrent();


        var fileSystemRule = new FileSystemAccessRule(currentUserIdentity,
                                                      FileSystemRights.FullControl,
                                                      InheritanceFlags.ObjectInherit |
                                                      InheritanceFlags.ContainerInherit,
                                                      PropagationFlags.None,
                                                      AccessControlType.Allow);


        directorySecurity.AddAccessRule(fileSystemRule);
        directoryInfo.SetAccessControl(directorySecurity);


    }

and getting the process is

    public static int GetIISProcessID(string appPoolName)
    {
        //return 0;

        string commandLine = String.Empty;
        Process[] pCollection = Process.GetProcessesByName(appPoolName);
        //Process.GetProcessById(7684, "w3wp.exe");
        //Process.GetProcessesByName("w3wp.exe");

        foreach (Process pInstance in pCollection)
        {

            ObjectQuery sq = new ObjectQuery

                ("Select CommandLine from Win32_Process Where ProcessID = '" + pInstance.Id + "'"); 
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq))
            {

                ManagementObjectCollection objectCollection = searcher.Get();

                foreach (ManagementObject oReturn in objectCollection)
                {

                    commandLine = oReturn["CommandLine"].ToString(); break;
                }

                Console.WriteLine(commandLine);
            }

        }
        return 0;

    }

can someone help me figuring out this how can i make a process access a folder.

  • Why don't you configure the IIS application pool to run with a specific user that has exclusive control over the folder? – Siraf Oct 06 '22 at 12:46

0 Answers0