11

Possiedo un'applicazione mvc4 con framework entità.Eliminazione del file, ma accesso negato

Voglio eliminare un file, ma ogni volta si dice:

Un'eccezione di tipo 'System.UnauthorizedAccessException' in mscorlib.dll, ma non è stata gestita nel codice utente

Ulteriori informazioni: Accesso al percorso 'G: \ Mijn Documents \ My Web Sites \ Lolabikes - Copia \ C# \ ContosoUniversity \ Images \' negato.

tramite questa riga: System.IO.File.Delete (percorso);

Questo è il metodo:

public ActionResult DeleteFiles(int id) 
     {   
       //var fileName = Path.GetFileName(id.FileName); 

       var DirSeparator = Path.DirectorySeparatorChar; 
       var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_'))); 
       var file = db.lolabikerPhotos.Find(id);    
       System.IO.File.Delete(path); 
       db.SaveChanges();   

      return Redirect(Url.Action("Edit", "Account") + "#tabs-3"); 

     } 

Ho appena eseguito l'applicazione in Visual Studio, in questo modo: http://localhost:41787/Account/Edit?UserId=hallo

ho già fatto le seguenti cose:

accesso completo alla carta , Ho aggiunto il servizio di rete alla mappa con il pieno controllo. Ma niente sembra funzionare. Sto usando Windows 7. E corro Visual Studio 2013 come amministratore

Vedo anche questo:

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. 

Qui potete vedere l'accesso:

enter image description here

provo qualcosa di simile this:

<system.web> 

    <identity impersonate="true" userName="Administrator" password="windowsPassword"/> 
    <httpRuntime requestValidationMode="2.0" maxRequestLength="1048576" executionTimeout="3600" /> 
    <compilation debug="true" targetFramework="4.5" /> 

    <pages validateRequest="false" /> 

    <!--<httpRuntime targetFramework="4.5" />--> 


    </system.web> 

Ho aggiunto questo:

<identity impersonate="true" userName="Administrator" password="windowsPassword"/> 

OK, posso eseguire l'applicazione, ma dà comunque l'errore:

Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' 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 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' 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 File 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. 

Source Error: 


Line 489:    var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_'))); 
Line 490:    var file = db.lolabikerPhotos.Find(id);    
Line 491:    System.IO.File.Delete(path); 
Line 492:    db.SaveChanges();   
Line 493: 

permesso completo:

enter image description here

scheda Avanzate: enter image description here

Modificato scheda autorizzazioni:

enter image description here

ho modificato il mio metodo di azione in questo modo:

public ActionResult DeleteFiles(int id) 
     {   
       var fileName = Path.GetFileName(@"\\Koala.jpg"); 

       var DirSeparator = Path.DirectorySeparatorChar; 
       var path = Server.MapPath(@"\\Images" + DirSeparator + fileName.Replace('+', '_')); 
       var file = db.lolabikerPhotos.Find(id); 
       LolaBikePhoto lola = db.lolabikerPhotos.Find(id); 
       db.lolabikerPhotos.Remove(lola); 
       System.IO.File.Delete(path); 


       db.SaveChanges();   

      return Redirect(Url.Action("Edit", "Account") + "#tabs-3"); 

     } 

E ora si sta lavorando!

+0

La maggior parte delle applicazioni viene eseguita con accesso utente regolare, indipendentemente dal fatto che l'account sia o meno un amministratore. Voglio solo assicurarmi che tu stia sicuramente eseguendo l'applicazione stessa come amministratore? Inoltre, assicurati che il file che stai eliminando non abbia il suo set di attributi di sola lettura. –

+0

Posso caricare i file, ma non eliminare i file, ho anche controllato il percorso e corretto Ho eseguito Visual Studio come amministratore –

+0

I update il mio post –

risposta

33

Ho anche avuto il problema, quindi mi inciampa su questo post. Ho aggiunto la seguente riga di codice prima e dopo una copia/cancella.

Eliminare

File.SetAttributes(file, FileAttributes.Normal); 
File.Delete(file); 

Copia

File.Copy(file, dest, true); 
File.SetAttributes(dest, FileAttributes.Normal); 
+0

Ciao Nader, ok, grazie. –

+1

Si prega di valutare la mia risposta –

+0

ok, ma vedere il mio post funziona, cosa ho fatto, ma la vostra soluzione può forse migliorare qualcosa, lo proverò –

2

Costruire sulla risposta - Per quanto mi riguarda ho dovuto impostare la cartella ei file al suo interno agli attributi normali. (in VB.NET)

Dim di As New DirectoryInfo("file path") 

    di.Attributes = FileAttributes.Normal 

    For Each fi As FileInfo In di.GetFiles 
     fi.Attributes = FileAttributes.Normal 
    Next