0

I have a little program to configure another program. using that program, it saves repository server details (repository path and username and password). Repository path is always a network path. so I validate that path using this piece of code. Directory.Exists(txtRepositaryPath.Text)

my question is there any way to give permission to above code. its simply return false always when no permission. is there a way to achieve this ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • To get access to a folder on the network you need to be able to see folder using a Window Explorer. The folder is really mounted on another PC (or File Server). So the following must exist 1) The folder must be shared on the remote machine 2) There must be a group account that is set up on both the remote machine and local machine 3) Your user account must be in the group on the remote machine and local machine. – jdweng Jun 25 '18 at 10:07
  • Thank you for responding.yes repository path is shared on the network and other 2 is not sure, may be possible. do have any code ? – Supun Rathnayaka Jun 25 '18 at 11:14
  • It is not code. It is the User Account Settings in Windows. And if you are on a corporate network than an MIS person needs to update the Group Policies. – jdweng Jun 25 '18 at 11:37

1 Answers1

0

You could add app.config file to take permission from user before your program run, so that application will run in Admin mode and will be able to access authorized folders.

Example

Read this to add application config file and in this way you see permission for program and replace this requestedPrivileges with

<requestedPrivileges>
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
        </requestedPrivileges>

You can check if directory is accessible or not using

  private bool hasWriteAccessToFolder(string folderPath)
  {
   try
   {
    // Attempt to get a list of security permissions from the folder. 
    // This will raise an exception if the path is read only or do not have access to view the permissions. 
    System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
       return true;
   }
    catch (UnauthorizedAccessException)
    {
       return false;
    }
 }
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
  • Changing Access permissions will not work if the Window Users Groups are not set up correctly. – jdweng Jun 25 '18 at 11:38