|
-
May 9th, 2013, 10:19 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Alternative to System.IO.File.Delete?
I'm having an issue (in ASP.NET but this is more of a general VB.NET question) where System.IO.File.Delete works on a page outside a gridview but not inside of it. I'm a bit tired of trying to figure out why. So...
...I was wondering is there an alternative method to deleting files in vb.net? I know there are usually 16 different ways to do any action, and I am kind of hoping I can find an alternative that works and stop wasting my time on this.
Thanks.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
May 9th, 2013, 10:21 AM
#2
Re: Alternative to System.IO.File.Delete?
How is it not working? I'm assuming some exception is being thrown, correct? If so, post the exception and your code (if possible) so we can work on this problem. There are alternates, but it's silly when it might be something simple that's stopping this from working.
-
May 9th, 2013, 10:27 AM
#3
Re: Alternative to System.IO.File.Delete?
I'm not sure that anything would work 'inside' a grid view. Pewrhaps you could explain exactly what that means as well!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 9th, 2013, 10:27 AM
#4
Thread Starter
Frenzied Member
Re: Alternative to System.IO.File.Delete?
 Originally Posted by formlesstree4
How is it not working? I'm assuming some exception is being thrown, correct? If so, post the exception and your code (if possible) so we can work on this problem. There are alternates, but it's silly when it might be something simple that's stopping this from working.
When I try to delete within the RowCommand, I get an Access Denied Error, outside, no Access Denied Error.
i.e. on the same page:
This works where txtTestDelete.Text is hardcoded to "C:\Documents\ACSDocs\Test Document.docx":
Code:
System.IO.File.Delete(txtTestDelete.Text)
This throws:
Access to the path 'C:\Documents\ACSDocs\' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\Documents\ACSDocs\' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Code:
Protected Sub gvFiles_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFiles.RowCommand
' Finish Me
' Restrict users to Stephen/Martin
If e.CommandName = "DeleteFile" Then
Dim oSQL As New clsSQL
Dim strFileName As String = ""
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
'Dim strFilePath As String = Server.MapPath("~\ACSDocs\" & strFileName)
Dim strFilePath As String = "C:\Documents\ACSDocs\" & strFileName
System.IO.File.Delete(strFilePath)
oSQL.DeleteACSDocs(gvFiles.DataKeys(e.CommandArgument).Value())
oSQL.LoadACSDocs()
gvFiles.DataSource = oSQL.Dataset.Tables("ACSDocs")
gvFiles.DataBind()
End If
End Sub
It is beyond frustrating. That's kind of why I am ready to try an alternative and move on.
Last edited by SeanGrebey; May 9th, 2013 at 10:31 AM.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
May 9th, 2013, 10:30 AM
#5
Thread Starter
Frenzied Member
Re: Alternative to System.IO.File.Delete?
 Originally Posted by dunfiddlin
I'm not sure that anything would work 'inside' a grid view. Pewrhaps you could explain exactly what that means as well!
Sorry, the Delete button embedded into each row and fired with a RowCommand.
Code:
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="docID">
<Columns>
<asp:ButtonField CommandName="DeleteFile" ButtonType="Link" Text="Delete" ItemStyle-ForeColor="Blue" />
<asp:TemplateField HeaderText="Description">
<ControlStyle />
<ItemTemplate>
<asp:HyperLink ID="hlDescription" runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="fileName" HeaderText="File Name" />
<asp:BoundField DataField="addedDate" HeaderText="File Added" />
</Columns>
</asp:GridView>
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
May 9th, 2013, 10:35 AM
#6
Re: Alternative to System.IO.File.Delete?
Every one of the 16 (alleged) methods will give you the same error in this circumstance. ASP is a web language and therefore does not have default access to the local file system (for rather obvious reasons!) This has nothing to do with the delete method, it's a security safe guard which prevents people setting up malicious websites that can wipe your harddrive without batting an eyelid!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 9th, 2013, 10:53 AM
#7
Thread Starter
Frenzied Member
Re: Alternative to System.IO.File.Delete?
 Originally Posted by dunfiddlin
Every one of the 16 (alleged) methods will give you the same error in this circumstance. ASP is a web language and therefore does not have default access to the local file system (for rather obvious reasons!) This has nothing to do with the delete method, it's a security safe guard which prevents people setting up malicious websites that can wipe your harddrive without batting an eyelid!
That's why you assign access to the AppPool Account to the specific Folder security options (that is in the file system I have assigned Network Service to have full control over that folder, and have set the Application Pool to run under the Network Service account. I can write files with no issue to the folder from the web page). And the page has no problem with access outside the gridview, just inside the gridview. It can delete fine with just a button and a text box on the page.
So my next step was to see if this was some quirkiness with System.IO.File.Delete embedded in the gridview.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
May 9th, 2013, 11:07 AM
#8
Thread Starter
Frenzied Member
Re: Alternative to System.IO.File.Delete?
SOB ok I figured it out. I feel pretty stupid now.
This issue is with this line:
Code:
oSQL.GetFileName(strFileName, gvFiles.DataKeys(e.CommandArgument).Value())
returning an empty string, so instead of trying to delete a file, I was trying to delete a directory and that was failing.
Thanks for the help.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|