0

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

このコードは私にこの例外をスローします:

タイプ 'System.IO.FileNotFoundException' の初回例外が mscorlib.ni.dll で発生しました ファイル 'C:\Data\Programs{9B9E8659-C441-4B00-A131-3C540F5CEE4F}\Install\main.lua' が見つかりません。

インストールディレクトリにファイルを追加するには?

4

2 に答える 2

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);
于 2013-04-05T12:08:50.033 に答える
1

プロジェクト ツリー内の特定のフォルダー (/Data など) にファイルを Content として追加することで、一部の電話アプリでこれを解決しました。次に、アプリを初めて実行するときに、コンテンツ ファイルを分離ストレージにコピーし、必要に応じてアプリが読み取ることができるようにします。簡単な例を次に示します。

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

このアプローチの欠点は、基本的にデータ ファイルが 2 回保存されることです。良い面は、隔離されたストレージに入ると、作業が非常に簡単になることです. とはいえ、Lua API が何をサポートしているのかはわかりません。つまり、Lua API が分離ストレージからロードできるかどうかはわかりません。そうでない場合は、いつでもファイル ストリームを開いて、その方法で Lua スクリプト ファイルをロードできます。

于 2013-03-29T02:01:42.050 に答える