簡體   English   中英

C#,Windows Phone 8安裝目錄

[英]C#, Windows Phone 8 install directory

如何在C#WindowsPhone8 SDK中添加文件以安裝目錄?

我正在嘗試讀取文本文件,該文件位於項目的Content目錄中。 問題是沒有文本導入程序。 但這沒關系。 真正的問題是,我不知道如何添加文件來安裝目錄。 內容添加的文件不起作用。

我試圖將Lua腳本保存在文本文件中,然后執行它。 我正在使用“ Aluminium 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);
    }
}

這段代碼拋出了這個異常:

mscorlib.ni.dll中發生類型為'System.IO.FileNotFoundException'的第一次機會異常。找不到文件'C:\\ Data \\ Programs {9B9E8659-C441-4B00-A131-3C540F5CEE4F} \\ Install \\ main.lua'。

如何添加文件到安裝目錄?

將文件作為內容添加到項目中。 您可以使用以下方式訪問文件:

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);

我已經通過將某些文件作為內容添加到項目樹中的特定文件夾(例如/ Data)中來解決了一些電話應用程序中的問題。 然后,當應用程序首次運行時,我將內容文件復制到隔離存儲中,然后我的應用程序可以根據需要讀取該文件。 這是一個簡單的示例:

// 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腳本文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM