Programatically grant access permission to user in C:\ to create textfiles - c#

I ran my program in C# that suppose to create and write a file in C:\, but an error occurred saying that access to the path C:\mytextfile.txt is denied. Is there any code that will give the user permission in C:\?

Yes by using the manifest http://www.codeproject.com/Articles/66259/Requesting-Admin-Approval-at-Application-Start or run as different account which has sufficient rights

You need to run the code as an administrator. You don't need C# code for this, you need to change how you're running the code.
However, I suspect that you're trying to do something wrong. I'd guess that you're trying to store some data locally, and are using the root of C: for simplicity, when you should actually be using the standard libraries to locate the users folder or to get a temporary file.
e.g. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
(for other folders, use Intellisense to look at the other SpecialFolder children.)

Related

c# window application system.data.oledb.oledbexception operation must use an updateable query

I have made a window application that works very well when i ran through but after creating its setup it is throwing below mentioned exception. I have try to give full access to the database file but still it is not working.
system.data.oledb.oledbexception operation must use an updateable.
query
i am using window 7 and installation folder is c:\program files\abc\ and access db is in same folder. Is this any issue of permissions? Please assist me to remove this exception.
There can be some permission issue just refer this Link
http://www.mikesdotnetting.com/Article/74/Solving-the-Operation-Must-Use-An-Updateable-Query-error
Make sure the ASPNET account (or whatever account is in use at the time) has
Change permissions to the directory where the .mdb file is located. Access
needs to write some temp and locking files during the operation.

'Access denied' to an xml file in windows application

In my .net windows application am using a xml file .
But after installing the application by creating setup, while I am running that 'Access Denied' to that xml file message is shown.
I am using System.IO.File methods for doing file operations.
If any body knows the solution pls share.
Write access to program directory has been more and more restricted (starting with XP/Vista) since that is a secruity risk!
I would suggest to have the "base version" of that file readonly in your program directory...
Upon start of your app you check whether it is present in ApplicationData or LocalApplicationData or CommonApplicationData - (to get the real path at runtime use Environment.GetFolderPath). If it is not there then copy it there - in those locations your application has write access by default with no need for Administrator rights.
Program Files folder has limited access and can be modified by Administrator account. I would suggest you to save your xml file in *C:\Users\YourPCName\AppData\Local\YourAppFolder* .
This way you will be able to access and modify the file
I had this problem once so I used Isolated Storage and that fixed my problem. It may or may not work for you depending on the nature of your situation, but here's a quick tutorial on how to use it:
http://www.codeproject.com/KB/dotnet/IsolatedStorage.aspx

C#: Get folder ownership problem

I am trying to get the ownership of a protected file in C#.
System.Security.AccessControl.DirectorySecurity Sec = new DirectorySecurity(directory, AccessControlSections.All);
is not working for me. I get the error UnauthorizedAccessException. So I cannot change the owner of the file and I cannot edit my access rights. Does anyone know a way how to set the directory permissions?
It sounds like you (or the user that you're application is running under) doesn't have access to the directory that you're trying to get access to.
Mike O'Brien published a nice blog post solving my problem:
http://blog.mikeobrien.net/2009/11/taking-ownership-and-setting-admin.html
This allows me to take over ownership of any folder.

Directory.Move() works in debug mode, but not when deployed

I have a webapp which has read/write/execute access to an aliased directory. When I am in debug mode in Visual Studio, the following statement works:
Directory.Move("\\\\localhost\\Alias\\oldDirectory","\\\\localhost\\Alias\\newDirectory");
The net result is that, oldDirectory is now newDirectory in the aliased directory.
But, when I'm testing this code in pre-production, I have oldDirectory and newDirectory in the aliased directory. Directory.Move is now behaving as if it is only copying oldDirectory to newDirectory.
Why is this happening?
This is most likely a permissions issue.
The user account that is executing this command probably has Create / Write permissions but not Delete permissions in the aliased directory. I would check whether the user account that the program is executing under has Delete / Delete Subfolders and Files permissions.
Edit:
To test this theory, I would temporarily grant the Users group Full Control over the folder to see if the problem goes away.
Make sure the folder is not under write-protection and no process is accessing any files at the moment you are trying to move the folder.
Also check whether you gave the security permissions to the correct user, by verifing under which user account the applicationpool is running.
You might also want to consider developing on a local IIS to prevent such situations in the future (I've been there; not nice)
I believe that, instead of using Directory (which is static) I used DirectoryInfo which solved my problem. I think that the heart of the issue was that Directory does more security checks than an instance of DirectoryInfo does. I'm still unclear why this is, but it seemed to work.

Accessing Text File in Non-Admin mode

I have a text file in Program Files. I cannot write it from a C# application while running in non-admin mode.
I am using this snippet
TextReader read = new StreamReader("C:\Program Files\......\link");
It is throwing an error that the access to the file is denied, however I can read it!
Thanks
Access to a file can be different for reading and writing. As a non-admin, it's normal to be able to read files in Program Files, but not be able to write them.
If the file is a setting for the current user, you should put it in a folder under the AppData folder. You can find the AppData folder's location by calling Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
If the file is a setting for all users on the computer, use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for a list of other possible special folder locations.
Non-admin users don't have write access to files in C:\Program Files by default. If you want to write to a file that is accessible to all users, you should create it in C:\ProgramData.
In .net there is a class File
through which you can use the method
File.read(file path)
this return a string
so you can easily manage it
it also works in a non admin mode

Categories

Resources