2

When I delete files manually using right click menu then it is deleted.

But when I run this code:

string[] filePaths = Directory.GetFiles(@"c:\My cloud\VM Instances\"); foreach (string filePath in filePaths) File.Delete(filePath); 

I got this error:

"A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Access to the path 'c:\My cloud\VM Instances\i-7e3cbb19 (manish).cvm' is denied. The thread '' (0xb5c) has exited with code 0 (0x0). A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll 'cloud_sync.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled."

So what should I do?

1 Answer 1

7

The process that your code is running has different permissions than your interactive login process. The big hint is that you get an UnauthorizedAccessException.

Is this a website? If so, the ASPNET worker process has by design very limited permissions by default (and it would be dangerous to stray far from that default).

Is this a Windows Service? If so, by default they also run as a user with very limited permissions.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for replying so fast. This is a window service. So how can i run my code with all permissions?
Thanks for replying so fast. This is a window service. So how can i run my code with all permissions?
@manish, consider asking separate question, or better yet do search for "windows service account permissions" on a search engine (like bing.com/search?q=windows+service+account+permissions ).
You probably don't want to just give you Windows Service all permissions across the board because someone who hacks your service gains too much access. Instead, segregate the code that needs elevated permission and run only that portion with additional permissions. See codeproject.com/Articles/105506/…
@EricJ. I am very new in this area and this is my 1st system service. I read that link but i didn't get what to do. I am using VS2010. Please help me out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.