.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to avoid System.UnauthorizedAccessException

Posted By:      Posted Date: September 23, 2010    Points: 0   Category :.NET Framework

I want to scan tree of directories and build list of all files. Root directory may contain sub-directories which I cannot access (for example 'C:\Users' directory at Vista or Win7 computer). Is it possible to completely avoid System.UnauthorizedAccessException in case of directories I have not right to access. Can I check directory access rights before call 'DirectoryInfo.GetSubdirectories(...)'.

Below is my code to demonstrate problem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestBuildFileList
  class Program
    // Method to traverse tree of directories and build list of files.
    public static void BuildListDataFileInfos(DirectoryInfo aRootDirectory, List<FileInfo> aResult)
      if (aRootDirectory.Exists)
        if ((aRootDirectory.Attributes & (FileAttributes.Hidden | FileAttributes.System)) != 0)

        // Collect results from sub-directories.
        DirectoryInfo[] directories = aRootDirectory.GetDirectories();
        foreach (DirectoryInfo di in directories)
          Program.BuildListDataFileInfos(di, aResult);

        // Add files from aRootDirectory
        FileInfo[] files = aRootDirectory.GetFiles();


View Complete Post

More Related Resource Links

How to avoid StackOverflowException when i use System.Windows.Markup.XamlWriter.Save

do following step, you can reappear the exception. step 1: Create a WpfApplication, named by "WpfApplication2" setp 2: Add two references, Microsoft.ReportViewer.Common and Microsoft.ReportViewer.WinForms step 3: paste the following code to MainWindow.xaml <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wf="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <WindowsFormsHost Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200" > <wf:ReportViewer Name="rp"></wf:ReportViewer> </WindowsFormsHost> </Grid> </Window> and paste the following code to MainWindow.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.M

'schema.AllCategories' threw an exception of type 'System.UnauthorizedAccessException

We're porting a solution from 2007 to 2010 and we're having trouble accessing the CrawledProperties property from the Schema object. Other properties from the Schema object however are accessible (e.g. AllManagedProperties). It does not matter whether we use the obsolete SearchContext to create the Schema object, or the new SearchServiceApplication (Example code from Steve Curran here: http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/2550e6e8-34a6-4012-969f-9657dc526d74) Now what's interesting is that the SAME user (Farm administrator) can access the AllCategories using a Windows Forms application, but when using a SharePoint solution (Codebehind on an application page) it doesn't work. We have double-checked that the executing user is the same. The same user can also browse the CrawledProperties using the Central Admin UI. We're experiencing this on several server installations, one of which is the "Standalone" installation of Search Server Express 2010, whereas the same user was used to perform the installation. I'd appreciate any ideas you might have.

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSD



System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

 at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)


Resolution Steps (there could be various causes for this error but this solution worked for me):

- Run Administrative Tools > Sharepoint Central Administration

- Go to Application Management > Sharepoint Site Management > Site Collection Administrations

- Check if your name (the person configuring sharepoint) is primary or secondary administration








I am using visual studio 2010

C#,web application............the OS i have is windows xp with iis 5.0

i try to delete few folders in the C drive using file functions :


But i shows up that  the access is denied and i am not allowed to access the files..............

for all the folders there is a check box saying read only...................i am not able to remove it externally nor within the code ..........................i gave all the permissions to the respective folders............. asp.net user and all...

but it still gives me the same damn error..

i would be very thank ful in how to solve this issue

Error: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_


Hi guys,

I am new to SSIS and I am trying to run a script task, I have set up variables for username, password, etc in my script, but when I execute the task, it fails with the error System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

Can anyone suggest anyway to remedy this issue.



MSMQ system.messaging.message.body unauthorizedaccessexception: Access is denied (exception fro HRRE



I am facing this error long back.. Please help me in this regards...

I have my .Net 2.0 Windows service to read the Priavate non transactions message queues and process into Sql Server 2005 database.....

I have used threading concept too.. i have multiple queues to be read on regular basis...

Getting below error exactly when reading the message body

system.messaging.message mInput ;

timespan tsInput = 1000;

mInput = mqInput.Receive(tsInput);

      ActiveXMessageFormatter axFormatter= new ActiveXMessageFormatter();
      pubqloader.DMPMessage DMPmsg = new PubQLoader.DMPMessage();
      object temp = new Object();
      temp = mInput.Body; --- here getting the error 

UnauthorizedAccessException Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))   

Stack Trace1: when mInput.Body is used

System.Messaging.Interop.NativeMethods.OleLoadFromStream(IStream stream, Guid& iid)
   at System.Messaging.ActiveXMessageFormatter.Read(Message message)
   at System.Messaging.Message.get_Body()

System.UnauthorizedAccessException when enumerating files in directory




as the title says, I've been getting a System.UnauthorizedAccessException when enumerating the files in a directory. The exact line that throws the error is:

string[] files = System.IO.Directory.GetFiles(path, "*.htm*");

My first thought was that I had insufficient NTFS privileges on the directory, but the error kept occuring even when I granted Full Access to Everyone. Running the program as Administrator (Win 7) produced the same result. Googling didn't give me any other ideas either. Anyone have a clue what the problem could be? Thanks.

Top .NET Performance Problems and how to avoid them

Every time I work with one of our .NET customers to help them with managing their application performance I come across the same problems as seen with other clients before: lots of ADO.NET queries, many hidden exceptions in core or 3rd party .NET libraries, slow 3rd party components, inefficient custom code

System Development Life Cycle

The Systems Development Life Cycle (SDLC) is a conceptual model used in project management that describes the stages involved in an information system development project from an initial feasibility study through maintenance of the completed application. Various SDLC methodologies have been developed to guide the processes involved including the waterfall model (the original SDLC method), rapid application development (RAD), joint application development (JAD), the fountain model and the spiral model. Mostly, several models are combined into some sort of hybrid methodology

The Agile System Development Life Cycle (SDLC)

The goal of this article is to describe the agile system development life cycle (SDLC), putting it in context from what you may have heard about within the agile community and more importantly within the context of your overall IT efforts.

How to detect and avoid memory and resources leaks in .NET application

Despite what a lot of people believe, it's easy to introduce memory and resources leaks in .NET applications. The Garbage Collector, or GC for close friends, is not a magician who would completely relieve you from taking care of your memory and resources consumption.

I'll explain in this article why memory leaks exist in .NET and how to avoid them. Don't worry, I won't focus here on the inner workings of the garbage collector and other advanced characteristics of memory and resources management in .NET.

Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch

Everybody who does performance optimization stumbles sooner or later over the Stopwatch class in the System.Diagnostics namespace. And everybody has noticed that the measurements of the same function on the same computer can differ 25% -30% in run time. This article shows how single threaded test programs must be designed to get an accuracy of 0.1% - 0.2% out of the Stopwatch class. With this accuracy, algorithms can be tested and compared.



hello i have the following problem

i have upload my content to hosting server but i get the following error

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SecurityException: Request for the permission of typ

Problem upgrading to MVC3 - System.Web.Routing



I'm trying to upgrade to .NET 4 and MVC 3.

When trying to compile my project, I get the error:

Error 1 The type or namespace name 'Routing' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) C:\Users\spencer\Desktop\weblauncher\Common\MVCConfig.cs 4 18 Common

The type or namespace name 'Routing' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

I do have the System.Web.Routing in my references for the project.

BUT - when I try to view System.Web.Routing in the OBJECT BROWSER, nothing is inside!  The System.Web.Routing namespace is not in there - the entire thing is empty!  Could it that the dll is corrupted?  I tried to reinstall the .NET 4 and MVC 3 stuff, but its still empty!

The dll is coming from here:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.Routing.dll

and the runti

System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPerm


Good Day all,

Having an issue with an outside user accessing my IIS7 box. I do not have this problem when running the website from my host machine. I found this post: Http://forums.asp.net/t/1371394.aspx. I assure you that this is not a solution because I am not storing any of my files on a network share. 

What do you think my approach should be. 

I already have read rights to IIS user to my BIN folder. 

Thanks for the help. 

System.IO.Packaging is not visible


I am new to ASP.net, I installed VS2005 and IIS 5 on my PC and set up my first website using IIS (not the embedded webserver in VS).

For some reason I cannot find the Assembly: "System.IO.Packaging", please let me know if I am allowed to use it in that medium and how can I reference it?

Thanks in advance.

how i can install sql server in my system


which edition of sql server  are best for me  Developer edition or express editon for development purpose.

i try to install last time sql server in my system but not successful so what i do for get the sql server in my system.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend