By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,214 Members | 1,956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,214 IT Pros & Developers. It's quick & easy.

Problem in Deleting Files

P: n/a
I am Developing a Web Application with ASP.NET 1.1
I have one project Folder which is virtual directory of IIS.
In this directory, I have one Folder named Photos in which I used to
Store Photos. On my Web Page, I have a button named delete which I used
to delete photo in the folder.
But when I click the button and apply the logic to delete the file, it
raises an Exception that the access to the file is denied. I am using
following code to delete the file. Please give me any suggestion.

FileInfo objectFile = new FileInfo(strFilePath); objectFile.Delete();

//This throws an Exception that Access to the file is denied on line
objectFile.Delete()

Thanks,
Sandeep Singh Sekhon
Jul 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

The .NET Framework runs all ASP.NET processes under the local ASPNET
account. You may have to grant write access to asp.net account to the folder
where you are deleting/ writing the file. The asp.net account varies
depending on which operating system the web server is hosted in
(xp/win2003).

-Hitesh Ramchandani.
"Sandeep Singh Sekhon" <ef*****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I am Developing a Web Application with ASP.NET 1.1
I have one project Folder which is virtual directory of IIS.
In this directory, I have one Folder named Photos in which I used to Store
Photos. On my Web Page, I have a button named delete which I used to
delete photo in the folder.
But when I click the button and apply the logic to delete the file, it
raises an Exception that the access to the file is denied. I am using
following code to delete the file. Please give me any suggestion.

FileInfo objectFile = new FileInfo(strFilePath); objectFile.Delete();

//This throws an Exception that Access to the file is denied on line
objectFile.Delete()

Thanks,
Sandeep Singh Sekhon

Jul 29 '06 #2

P: n/a
Thanks for Hitesh's inputs.

Hi Sandeep,

For such file deleting Access_Denied error, it has the following possible
causes:

1. The current ASP.NET application's running security identity doesn't have
the sufficient permission to delete the target file. Genernally if you're
not using impersonate in your ASP.NET application, the current security
context of the application is the worker process's identity. You can check
your ASP.NET application's worker process identity according to the
following MSDN article:

#Configuring ASP.NET Process Identity
http://msdn2.microsoft.com/en-us/library/dwc1xthy.aspx

Also, a simple means to get the current process identity is execute the
following code in your page to printout the current security context:

==========
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>Security Identity: " +
System.Security.Principal.WindowsIdentity.GetCurre nt().Name);
}
===========

After you've determined the security identity, verify that whether this
identity(account) has the sufficient permission to manipulate the target
files or folder).

BTW, for tracing file access permission issue, the filemon utility is quite
common and useful:

http://www.sysinternals.com/utilities/filemon.html

2. Suppose the security identity has the sufficient permission to
manipulate the target file, it is possible that the file's filehandle is
locked by some other process which make the ASP.NET process fail to acquire
the file handle. To check file handle lock/ownership, the processexplorer
utility is a helpful one:

#How To Determine File Handle Ownership
http://support.microsoft.com/kb/q232830/

http://www.sysinternals.com/Utilitie...sExplorer.html

Please have a look at the above things. If you have anything unclear or if
you meet any further problems, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to

http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial

response from the community or a Microsoft Support Engineer within 1
business day is

acceptable. Please note that each follow up response may take approximately
2 business days

as the support professional working with you may need further investigation
to reach the

most efficient resolution. The offering is not appropriate for situations
that require

urgent, real-time or phone-based interactions or complex project analysis
and dump analysis

issues. Issues of this nature are best handled working with a dedicated
Microsoft Support

Engineer by contacting Microsoft Customer Support Services (CSS) at

http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Jul 31 '06 #3

P: n/a
Hitesh Ramchandani wrote:
The .NET Framework runs all ASP.NET processes under the local ASPNET
account. You may have to grant write access to asp.net account to the folder
where you are deleting/ writing the file. The asp.net account varies
depending on which operating system the web server is hosted in
(xp/win2003).

-Hitesh Ramchandani.
"Sandeep Singh Sekhon" <ef*****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I am Developing a Web Application with ASP.NET 1.1
I have one project Folder which is virtual directory of IIS.
In this directory, I have one Folder named Photos in which I used to Store
Photos. On my Web Page, I have a button named delete which I used to
delete photo in the folder.
But when I click the button and apply the logic to delete the file, it
raises an Exception that the access to the file is denied. I am using
following code to delete the file. Please give me any suggestion.

FileInfo objectFile = new FileInfo(strFilePath); objectFile.Delete();

//This throws an Exception that Access to the file is denied on line
objectFile.Delete()

Thanks,
Sandeep Singh Sekhon

hi Hitesh,
I am using Windows XP Prof. How can I grant write access to
ASP.NET account for deleting the file.
Thanks,
Sandeep
Aug 1 '06 #4

P: n/a
Hi Sandeep,

Have you had a chance to have a look at my previous reply ,either? In my
last message, I've listed some suggestion on how to determine the ASP.NET
application's worker process identity or security context. Then, checking
whether that identity/account has sufficient permission to modify the
target directory. In addition, the "filemon" utility I mentioned earlier
is a good tool for tracing file access failure (you can find which account
failed to access which file on the machine). In case you haven't found my
last reply, here is a copy from the former message:
################################################## ###########
Hi Sandeep,

For such file deleting Access_Denied error, it has the following possible
causes:

1. The current ASP.NET application's running security identity doesn't have
the sufficient permission to delete the target file. Genernally if you're
not using impersonate in your ASP.NET application, the current security
context of the application is the worker process's identity. You can check
your ASP.NET application's worker process identity according to the
following MSDN article:

#Configuring ASP.NET Process Identity
http://msdn2.microsoft.com/en-us/library/dwc1xthy.aspx

Also, a simple means to get the current process identity is execute the
following code in your page to printout the current security context:

==========
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>Security Identity: " +
System.Security.Principal.WindowsIdentity.GetCurre nt().Name);
}
===========

After you've determined the security identity, verify that whether this
identity(account) has the sufficient permission to manipulate the target
files or folder).

BTW, for tracing file access permission issue, the filemon utility is quite
common and useful:

http://www.sysinternals.com/utilities/filemon.html

2. Suppose the security identity has the sufficient permission to
manipulate the target file, it is possible that the file's filehandle is
locked by some other process which make the ASP.NET process fail to acquire
the file handle. To check file handle lock/ownership, the processexplorer
utility is a helpful one:

#How To Determine File Handle Ownership
http://support.microsoft.com/kb/q232830/

http://www.sysinternals.com/Utilitie...sExplorer.html

Please have a look at the above things. If you have anything unclear or if
you meet any further problems, please feel free to post here.

Sincerely,

Steven Cheng
################################################## #########

Please feel free to let me know if you have anything unclear here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Aug 1 '06 #5

P: n/a
Hi Sandeep,

Have you got any progress on this issue? Please feel free to post here if
there is still any problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Aug 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.