2013-03-28 2 views
0

C# WindowsPhone8 SDK의 설치 디렉터리에 파일을 추가하는 방법은 무엇입니까?C#, Windows Phone 8 설치 디렉터리

내 프로젝트의 컨텐츠 디렉토리에있는 텍스트 파일을 읽으려고합니다. 문제는 텍스트 가져 오기 도구가 없다는 것입니다. 그러나 그것은 중요하지 않습니다. 진짜 문제는 파일을 설치 디렉토리에 추가하는 방법을 모르겠다는 것입니다. 내용이 추가 된 파일이 작동하지 않습니다.

나는 텍스트 파일에 Lua 스크립트를 저장하려고 시도하고있다. '알루미늄 루아'라이브러리를 사용하고 있습니다.

if (runAtStartup == false) 
{ 
    runAtStartup = true; 

    try 
    { 
     prs = new AluminumLua.LuaParser(ctx, "main.lua"); 
     prs.Parse(); 
    } 

    catch (Exception e) 
    { 
     System.Diagnostics.Debug.WriteLine(e.Message); 
    } 
} 

이 코드는 저에게이 예외를 throw 형식 'System.IO.FileNotFoundException'형식의 첫째 예외 파일 'C를 찾을 수 없습니다 mscorlib.ni.dll 에서 발생

: \ 데이터 \ Programs {9B9E8659-C441-4B00-A131-3C540F5CEE4F} \ Install \ main.lua '에 있습니다.

어떻게 파일을 설치 디렉토리에 추가 하시겠습니까?

답변

1

내 폰 트리의 특정 폴더 (예 :/데이터)에 콘텐츠로 파일을 추가하여 일부 휴대 전화 앱에서이를 해결했습니다. 그런 다음 앱이 처음 실행될 때 콘텐츠 파일을 격리 된 저장소에 복사하여 내 앱이 필요에 따라 읽을 수있게합니다. 다음은 간단한 예입니다.

// Check for data files and copy them to isolated storage if they're not there... 
// See below for methods found in simple IsolatedStorageHelper class 
var isoHelper = new IsolatedStorageHelper(); 

if (!isoHelper.FileExists("MyDataFile.xml")) 
{ 
    isoHelper.SaveFilesToIsoStore(new[] { "Data\\MyDataFile.xml" }, null); 
} 

/* IsolatedStorageHelper Methods */ 

/// <summary> 
/// Copies the content files from the application package into Isolated Storage. 
/// This is done only once - when the application runs for the first time. 
/// </summary> 
public void SaveFilesToIsoStore(string[] files) 
{ 
    SaveFilesToIsoStore(files, null); 
} 

/// <summary> 
/// Copies the content files from the application package into Isolated Storage. 
/// This is done only once - when the application runs for the first time. 
/// </summary> 
public void SaveFilesToIsoStore(string[] files, string basePath) 
{ 
    var isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    foreach (var path in files) 
    { 
     var fileName = Path.GetFileName(path); 

     if (basePath != null) 
     { 
      fileName = Path.Combine(basePath, fileName); 
     } 

     // Delete the file if it's already there 
     if (isoStore.FileExists(fileName)) 
     { 
      isoStore.DeleteFile(fileName); 
     } 

     var resourceStream = Application.GetResourceStream(new Uri(path, UriKind.Relative)); 

     using (var reader = new BinaryReader(resourceStream.Stream)) 
     { 
      var data = reader.ReadBytes((int)resourceStream.Stream.Length); 

      SaveToIsoStore(fileName, data); 
     } 
    } 
} 

이 접근 방식의 단점은 본질적으로 두 번 저장된 데이터 파일이 있다는 것입니다. 장점은 고립 된 저장소에 있으면 작업하기가 매우 쉽다는 것입니다. Lua API가 지원하는 항목을 알지 못합니다. 즉, 격리 된 저장소에서로드 할 수 있는지 여부를 알 수 없습니다. 그렇지 않다면 항상 파일 스트림을 열어서 Lua 스크립트 파일을로드 할 수 있습니다.

+0

안녕하세요, 답변을 주셔서 감사합니다. 이 코드를 어디에 넣어야 만 제 앱을 처음 실행할 때만 작동하게 할 수 있습니까? –

+0

추 신 : 나는 'Application'클래스에 문제가있다. Windows Phone SDK 8.0에서 사용할 수없는 System.Windows 네임 스페이스에 있습니다. –

+0

제 경우에는 파일에 액세스하기 전에 격리 된 저장소에서 파일의 존재 여부를 확인합니다. 거기에 없으면 위의 코드를 호출합니다. 그렇지 않으면 격리 된 저장소를 평소처럼 호출합니다. 그러나 Application_Launching 이벤트의 App.xaml.cs 파일에 추가 할 수도 있습니다. 파일 내에서 Isolated Storage에 파일의 존재 여부를 확인하고 위 코드가 아직없는 경우이를 호출하십시오. –

2

파일을 프로젝트의 컨텐츠로 추가하십시오. 당신은 당신의 파일에 액세스 할 수 있습니다 :이 같은

string folder = Package.Current.InstalledLocation.Path; 
string path = string.Format(@"{0}\data\myData.bin", folder); 
StorageFile storageFile = await StorageFile.GetFileFromPathAsync(path); 
Stream stream = await storageFile.OpenStreamForReadAsync(); 

또는 무언가 :

string folder = Package.Current.InstalledLocation.Path; 
string currentMovieVideoPath = string.Format(@"{0}\media\video\Movie.mp4", folder); 
this.MovieVideo.Source = new Uri(currentMovieVideoPath, UriKind.Absolute);