Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello, I'm trying to setup a folder with full permission, i tried this:
C#
AddDirectorySecurity(txtDestination.Text + @"Minecraft", "System", FileSystemRights.FullControl, AccessControlType.Allow);

but it wont work, it just gives me this:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Temp\Minecraft' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Uninstaller.ZIPFinder.button1_Click(Object sender, EventArgs e) in C:\Users\Daniel\documents\visual studio 2010\Projects\Uninstaller\Uninstaller\ZIPFinder.cs:line 363
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.239 (RTMGDR.030319-2300)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Uninstaller
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Users/Daniel/Desktop/Uninstaller/Uninstaller.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.235 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.258 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
ZipForge
    Assembly Version: 3.0.2.0
    Win32 Version: 3.0.2.0
    CodeBase: file:///C:/Users/Daniel/Desktop/Uninstaller/ZipForge.DLL
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.


any idea why? it should give the permissions..
Posted
Comments
Ed Nutting 26-Feb-12 14:07pm    
Your exception says your application (which won't be running as you (the user) so won't have same permissions) is trying to access a folder that it doesn't have access to so of course it won't be able to set full rights for it! See line 4 of your exception message. Now solve this by logging in as an admin and giving full permissions to the folder manually, not through code - seems like an awful lot of work just to play Minecraft....
NextGenDeveloper 26-Feb-12 14:25pm    
Didnt really understand it can you help me out with this?
Ed Nutting 26-Feb-12 14:27pm    
See Dave's solution - it said what I was trying to say...
Wonde Tadesse 26-Feb-12 16:56pm    
What OS is this. Setting permission on Vista, Window 7... is different than previous OS.

Since the code runs as the user who launched it, what it's telling you is that the user doesn't have the proper permissions to set permissions on that folder. Chances are a normal user account is only going to have Read and List Folder access to the C:\Temp folder, and therefore cannot create the Minecraft folder nor set permissions on it.

You're going to have to find a more suitable place to create this folder. Such as the one specified by the environment variable "TEMP". On Windows 7, that usually points to C:\Users\<userid>\AppData\Local\Temp, on which the user has most permissions to the folder. They can create/read/write any folder or file they want in there, but cannot modify permissions on them.
 
Share this answer
 
Comments
Ed Nutting 26-Feb-12 14:27pm    
Good answer - what I was trying to say in my comment but put better. My 5+ :)
NextGenDeveloper 26-Feb-12 14:35pm    
Doest work, still the same problem.
Access to the path 'C:\Users\AppData\Local\Temp\Minecraft is not allowed ;(
Dave Kreskowiak 26-Feb-12 17:23pm    
Reread my post. I just got done saying the user can create the files and folders in there but can NOT set permissions on them. Users normally cannot set permissions on anything.
Sergey Alexandrovich Kryukov 26-Feb-12 19:09pm    
Agree, a 5.
In my answer I suggested to use legitimate directories and explained how to get them. This is always better them messing with permissions.
--SA
Let me give you one simple advice: The directory "C:\Temp\Minecraft" is not a legal access-by-application folder under Windows 7. Is it a hard-coded path name? There are no situations when such names can be useful.

You should always calculate a valid directory for your operations during runtime.

First of all, you can find out a temporary directory for a user currently running the application using System.IO.Path.GetTempPath:
http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx[^].

You can create a separate subdirectory in this directory:
http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory.aspx[^].

For other purposes, you can find a directory dedicated for application settings for a current user of "all users" directory. Use System.Environment.GetFolderPath:
http://msdn.microsoft.com/en-us/library/dd992682.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

Depending on you choice, it will persist your application data on a per-user basis or make them common.

—SA
 
Share this answer
 
v2
Comments
NextGenDeveloper 26-Feb-12 14:50pm    
I tried the GetTempPath, now i get this error:


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.
at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
at System.Security.Principal.NTAccount.Translate(Type targetType)
at System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
at System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
at Uninstaller.ZIPFinder.AddDirectorySecurity(String FileName, String Account, FileSystemRights Rights, AccessControlType ControlType) in C:\Users\Daniel\documents\visual studio 2010\Projects\Uninstaller\Uninstaller\ZIPFinder.cs:line 427
at Uninstaller.ZIPFinder.button1_Click(Object sender, EventArgs e) in C:\Users\Daniel\documents\visual studio 2010\Projects\Uninstaller\Uninstaller\ZIPFinder.cs:line 298
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.239 (RTMGDR.030319-2300)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Uninstaller
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/Daniel/Desktop/Uninstaller/Uninstaller.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.235 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.258 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
ZipForge
Assembly Version: 3.0.2.0
Win32 Version: 3.0.2.0
CodeBase: file:///C:/Users/Daniel/Desktop/Uninstaller/ZipForge.DLL
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.233 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitdebugging="true">


When JIT debugging is enabled, any unhandled exception
will be sent to the JIT d
Sergey Alexandrovich Kryukov 26-Feb-12 16:58pm    
Strange thing, but it does not say much unless you make a code sample to show the problem. Anyway, you stack trace does not show GetTempPath, the problem is somewhere else.
Indicate exactly the line where the exception was thrown. Use the Debugger.
Did you do anything unusual with config?

Look, you don't need to modify access at all. Just use my advice.
--SA
Sergey Alexandrovich Kryukov 26-Feb-12 19:11pm    
May be you did not get it. My advice was using legitimate directories. If you do this in a right way, you simply don't need to mess with permissions.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900