Access Denied in creating directory using C# - c#

string windowsDirectory = Environment.GetEnvironmentVariable("ProgramFiles");
string mydirecoty = windowsDirectory + "\\" + "NetServices\\";
if (!Directory.Exists(mydirecoty))
{
Directory.CreateDirectory(mydirecoty); //Access to the path 'C:\Program Files (x86)\NetServices\' is denied.
}
exception is thrown when I run my .net application without Administrator account. While If I try to create directory in other then C:\ drive like D:\ it runs fine.
What is solution that I want to create directory in Program Files/ Windows folder?

The C:\program files folder is protected (by design) in Vista and Windows 7 (and Windows Server 2008 / 2008 R2) - normal user accounts do not have any permission to create directories in there - it's a system folder.
Either you need to run as admin - then you have permission to create directories even in protected system folders - or you create the directories elsewhere and not inside a protected system folder. The second option would be the recommended and preferred option.

If your application is intended to be run by the user, write to the user's app data folder or temp folder. Your app should install to the Program Files directory, and perhaps use some files in the Windows directory, but it should never have to write to either location.
You can easily get the user's app daa folder from the environment variables.

if you are running this from web application make sure that the application pool user has ntfs permission of that folder.

You need to elevate user privileges as it is a feature in windows vista/7.
Running as admin will solve the problem.

if you use operating systems higher than Windows XP, you will not able to access C: \ PrpgramFiles protecded because reading and writing, unless AVII your application with Administrator rights.
Bye

Related

Why my .exe file doesn't work when I install the program?

I have a problem when I install my Windows Forms App in a computer. The .exe file doesn't do anything when I click it. To start the aplication, I have to run the program as administrator.
It is developed in C#, .Net Framework 4.7.2 and I install it in a computer with Windows 10.
THanks!
You should not save anything to the application folder i program files. You should use a separate folder for any data that might change. There are a few locations to chose from:
ProgramData contains application data that is not user specific.This data will be available to all users on the computer. Any global data should be put in here.
AppData folder contains configuration settings, downloaded
information/files for a particular user. So, for example any user
specific preferences and profile configurations can be stored in the
AppData folder. The AppData folder is further divided into three
subfolders
Roaming - This folder contains data that can move with your
user profile from a computer to another.
Local - This folder contains
data that will not move with your user profile.
LocalLow - You can put
in lowlevel access information such as information related to web
browser running in a protected mode in this folder.
source
The location for these folders can be accessed by using Special Folders
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

.NET Core C# - Delete file in a symlink folder in Windows 10

I'm testing a web app locally that will be hosted in a Linux environment. For testing, I created a symlink folder using the administrator command-line and mklink /D. This link is set inside the wwwroot folder of my app and is linked to a directory in C:\Users\User\the_folder. Reason for doing this is because I will need to create a symlink on my linux server to a mounted block storage drive.
I'm able to write files and new directories to this symlink directory, but for some reason it won't allow me to delete a file. I AM, however, able to delete an entire directory along with its contents with System.IO.Directory.Delete(path, true);
To delete a file, I'm trying to use System.IO.File.Delete, but this returns an access denied error.
Does anyone know what the problem might be?

Accessing folders ouside of root IIS

I need to write files into a folder using c# that is outside of root folder on IIS 7.
I have made the folder and given IIS_IUSRS and the app pool users all rights on the folder but I always get UnauthorizedAccessException when I try to write to it.
Everything I googled says the solution is virtual directory, but I cant have the folder emptied every time I publish the web.
Is there a solution to this?
You also need to grant file access permissions to the NETWORK SERVICE account.

Access Program Files folder from users group using .NET code

I searched about this on internet but could not find exact answer.
In my application, I have a functionality which copies the files in folder (say pqr) to the location where user wanted. The location of source folder is the location of installed application (say c:\Program Files\abc\pqr).
When user is logged in to the machine with 'administrator rights', user is able to use this functionality. But when the user is logged in to the machine with having 'User rights' (non-admin user), this functionality throws exception that access to c:\Program Files\abc\pqr folder is denied.
I tried to elevate user privileges by using below attributes to copy method:
[PrincipalPermission(SecurityAction.Demand, Role = #"BUILTIN\Administrators")]
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
I also tried adding manifest file with below changes:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
But none of the approach worked.
Then I installed my application on D drive. And after that when I tried with the non-admin user the functionality worked as expected.
So when the application is installed in C:\Program Files, functionality was not working for non-admin user.But after installing the application to another location it worked for non-admin user.
So my question is, is it possible to give the rights if C:\Program Files to non-admin user programmatically in the application or we need to have admin users for using this functionality?
Changing the manifest is most simple way to elevate privileges. Change it in VS. But it is bad practice to require admin privileges for regular software.
You can make program folder accessible for all users during install. For example it can be made with InnoSetup with Permissions: users-modify in [Files] group.
If the source files are in Program Files and you copy in another location, probably the problem is in wrong file access when opening source files. Open for reading only.
var startInfo = new ProcessStartInfo("yourApplication.exe") { Verb = "runas" };
Process.Start(startInfo);
Environment.Exit(0);
or
application.exit();
if you are using winforms
but be careful to excute this code one time because if you put it in the load event you will get overflow in memory

Self-Updating .NET client application which needs to write in the Program File folder

Similar to: Request Windows Vista UAC elevation if path is protected?
I have a .NET Client Application installed in c:\Program Files (Windows Vista). This application should update itself, but it doesn't because of permission issues. The auto-updater should simply replace a couple of assemblies, but they are all located under c:\Program File and the application throws the following exception:
System.UnauthorizedAccessException:
Access to the path 'C:\Program
Files...' is denied.
I have no control on where the application could be installed and the permission. Is there any workaround for this? Is it possible to request the Administrator rights for a couple of seconds? Is it possible to pop a UAC window? I am pretty sure that there a workaround... Otherwise, how Firefox would be able to update itself?
Thanks in advance for the help and ideas!
Could you use a Click Once deployment method? We use this for an internal application and users have no problems with permissions when we publish a new version. They are prompted to install the update when they launch the app (if a new version exists) and it installs without a hitch.
You can't elevate a process's permissions halfway through, but you can start up another separate process with higher permissions that can do the work for you.
Get your main application to put all the files / installation details into a low-permission temporary location. When you're ready, start up a smaller application whose only job is to copy over those files to the Program Files directory (and maybe restart your main application with the new updates). Mark that application as requiring the needed permission to copy to the Program Files directory or write to the registry (or whatever else is needed).

Categories