通过将文件作为内容添加到我的项目树中的特定文件夹(例如/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 脚本文件。