2013-07-23 3 views
0

ContextMenu의 MenuItem을 탭하면 & 디렉토리를 삭제하려고합니다. 그러나 나는 파일/디렉토리가 삭제되지 않기 때문에 이슈를 다루는 것으로 보인다.디렉토리의 모든 파일을 삭제하고 디렉토리 자체를 삭제하는 데 문제가 있음

그러나 오류가 발생하지는 않지만 작동하지 않는 것 같습니다.

여기 내 코드는 지금까지의 :

private void gridSessionDelete_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
{ 
    var item = (((sender as MenuItem).Parent as ContextMenu).Owner as Grid); 
    var title = (TextBlock)item.FindName("Title"); 
    string directory = title.Text; 

    var appStorage = IsolatedStorageFile.GetUserStoreForApplication(); 

    string[] fileList = appStorage.GetFileNames(directory + "\\*"); 

    foreach (string file in fileList) 
    { 
     appStorage.DeleteFile(directory + "\\" + file); 
    } 

    appStorage.DeleteDirectory(directory); 

    bindList(); 
} 

사람이 내가 잘못하고있는 무슨에 어떤 도움이됩니까?

감사합니다. 감사합니다.

+0

이 스레드는 도움이 될 수 있습니다 : http://stackoverflow.com/questions/6801766/exception-when-trying-to-delete-a-directory-in-isolated-storage – anderZubi

+0

불행하게도 보이지 않는 문제에 대해 많은 것을 밝힙니다. – Newbie

답변

0

음, 오류가있는 곳을 몇 군데 볼 수 있습니다.

첫째,이 라인 : 당신이 등의 키워드를 가지는 형태를 캐스팅 할 때

아시다시피
var item = (((sender as MenuItem).Parent as ContextMenu).Owner as Grid); 

, 결과는 널 (NULL)이 될 수 있으며 예외가 throw되지 않습니다.

가장 중요한 IMO 둘째, 및 :

이 줄 :

string[] fileList = appStorage.GetFileNames(directory + "\\*"); 

아무것도 찾을 수 없습니다. 검색 요청에서 "*"(별표) 대신 ""(별표 - 별표)를 사용해야합니다. 또한

, 당신이 IsolatedStorage 작업 할 때처럼, 키워드를 사용하여 를 사용

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       // your code 
      } 
+0

어, 답변에 *. * (별표 - 성급)와 같은 표현을 쓰는 방법을 아는 사람이 있습니까? formating 후 이탤릭체 텍스트처럼 보입니다. – Olter

+0

이전 코드에서 나는'string [] fileList = appStorage.GetFileNames (directory + "\\ *")'결과를 반환했다. 그래서이 시나리오에서 왜 작동하지 않아야하는지 모르겠다. – Newbie

0

코드가 실제로 실행합니까? 중단 점에 충돌이 발생합니까? 그렇다면 여기에 디렉토리의 모든 파일을 삭제하는 데 사용하는 코드가 있습니다. 그것은 나를 위해 일한다. 내가 보는 주된 차이점은 DeleteFile 메서드에 있습니다.

var storage = IsolatedStorageFile.GetUserStoreForApplication(); 
if (storage.DirectoryExists(directoryName)) 
{ 
    foreach (var oldFile in storage.GetFileNames(string.Concat(directoryName, "\\*"))) 
    { 
     storage.DeleteFile(Path.Combine(directoryName, oldFile)); 
    } 
} 
+0

System.IO.Path.Combine을 사용해 보았지만 아무런 차이가없는 것 같습니다. – Newbie

+0

"_Click"이벤트가 아니라'_Tap' 이벤트를 사용했기 때문에 당황스럽게도 그 사실을 깨달았습니다. 어떤 이유로 'ContextMenu'가 Tap hahaaa를 발생시키지 않았습니다. – Newbie

+0

위의 주석에 대해 언급 했어야합니다. 내 코드는 결국 정상적으로 작동했습니다. – Newbie

관련 문제