2015-01-01 2 views
-3

안녕하세요 기본적으로 내 % appdata % 폴더를 삭제할 수있는 응용 프로그램에서 단추를 만들려고하지만 읽는 유일한 파일을 삭제할 수 없다는 문제가있어서 인터넷 검색을하기로 결정했습니다. 그러나 문제는 여전히 어쨌든 계속됩니다. 나의 최근 시도는 어떤 단서도 아직 작동하지 않았습니까?C# 읽기 전용 파일 삭제

삭제하려는 것은 % appdata %/test이며 하위 폴더도 있습니다.

private void ClearButton_OnClick(object sender, RoutedEventArgs e) 
    { 
     string filepath = (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test")); 
     //Get Currently Applied Access Control 
     FileSecurity fileS = File.GetAccessControl(filepath); 

     //Update it, Grant Current User Full Control 
     SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; 
     fileS.SetOwner(cu); 
     fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow)); 

     //Update the Access Control on the File 
     File.SetAccessControl(filepath, fileS); 

     //Delete the file 
     File.Delete(filepath); 
     Process.Start(Application.ResourceAssembly.Location); 
     Environment.Exit(0); 
    } 
+0

이 방법의 결과는 무엇입니까? –

+2

[읽기 전용 파일을 삭제하는 방법은 무엇입니까?] (http://stackoverflow.com/questions/265896/how-do-i-delete-a-read-only-file) – Cyral

+0

@commusoft http : //i.imgur.com/wdBeCH3.png – MrRdy2Lose

답변

0

는 또한

using System.IO; 
using System.IO; 
using System.Security.AccessControl; 
using System.Security.Principal; 

//Get Currently Applied Access Control 
FileSecurity fileS = File.GetAccessControl(filepath); 

//Update it, Grant Current User Full Control 
SecurityIdentifier cu = WindowsIdentity.GetCurrent().User; 
fileS.SetOwner(cu); 
fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow)); 

//Update the Access Control on the File 
File.SetAccessControl(filepath, fileS); 

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

이 없다면 당신은 당신이 관리자 권한으로 응용 프로그램을 실행해야합니다, 먼저 소유권 가져 할 필요가있을 수 있습니다. 이를 위해

, 오른쪽 추가 프로젝트를 클릭 ->새 항목 ->응용 프로그램 매니페스트 파일

다음이 라인이

<requestedExecutionLevel level="requireAdministrator" uiAccess="true" /> 

<requestedExecutionLevel level="asInvoker" uiAccess="false" /> 

교체

응용 프로그램이 실행됩니다. 관리자 권한으로

+0

작동하지 않았으므로 같은 오류가 발생했습니다. http://i.imgur.com/SatqfJ9.png – MrRdy2Lose

+0

@ MrRdy2Lose 답변을 편집했습니다. 지금 확인하십시오. – SimpleGuy

+0

http :// /i.imgur.com/tunqoge.png – MrRdy2Lose

0

관리자 권한으로 응용 프로그램을 실행 해보십시오. C 드라이브에는 관리자 권한이 필요합니다.

+0

http : //i.imgur .com/XclRZ12.png – MrRdy2Lose

관련 문제