외부 응용 프로그램에서 읽을 수있는 파일을 작성하고 싶지만 일부는 IsolatedStorage 장점이 있지만 기본적으로 예기치 않은 예외에 대비해야합니다. 가질 수 있을까요?IsolatedStorage 파일의 경로를 가져 와서 외부 응용 프로그램에서 읽을 수 있습니까?
15
A
답변
23
리플렉션을 사용하여 IsolatedStorageFileStream
클래스의 비공개 필드에 액세스하여 디스크에서 격리 된 저장소 파일의 경로를 검색 할 수 있습니다. 다음은 그 예입니다.
// Create a file in isolated storage.
IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Create, store);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Hello");
writer.Close();
stream.Close();
// Retrieve the actual path of the file using reflection.
string path = stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream).ToString();
확실한 방법은 아닙니다.
디스크의 위치는 운영 체제의 버전에 따라 다르며 다른 응용 프로그램이 해당 위치에 액세스 할 수있는 권한이 있는지 확인해야합니다.
6
대신 위치를 임시 파일을 생성하고 얻을 당신이 직접 상점에서 경로를 얻을 수 있습니다 :
var path = store.GetType().GetField("m_RootDir", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(store).ToString();
8
내가하여 FileStream의 이름 속성을 사용합니다.
private static string GetAbsolutePath(string filename)
{
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
string absoulutePath = null;
if (isoStore.FileExists(filename))
{
IsolatedStorageFileStream output = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
absoulutePath = output.Name;
output.Close();
output = null;
}
return absoulutePath;
}
이 코드는 Windows Phone 8 SDK에서 테스트되었습니다.
+1
"Desktop".Net 4.5에서 이름이 "알 수 없음"입니다. – habakuk
적어도 Silverlight 4에서는이 리플렉션을 수행하려는 모든 시도가 발생합니다. mscorlib.dll에서 'System.FieldAccessException'유형의 첫 번째 예외가 발생했습니다. 추가 정보 : 메서드로 시도 'Comms.MainPage.LayoutRoot_Loaded (System.Object, System.Windows.RoutedEventArgs) ''System.IO.IsolatedStorage.IsolatedStorageFile.m_StorePath '필드에 액세스하지 못했습니다. 또한 "m_FullPath"가 아닌 "m_StorePath"가되었습니다. 사용하지 않는 이유가 더 있습니다. – DJA
@DJA 보안 문제로 인해 Silveright에서 비공개 멤버를 얻을 수 없기 때문입니다. – ghord