Windows phone 7 如何从项目文件夹中读取文件?

Windows phone 7 如何从项目文件夹中读取文件?,windows-phone-7,Windows Phone 7,当我的应用程序第一次在windows phone上启动时,我想从项目文件夹中获取一些文件(xml/图像),并将它们写入隔离存储 我如何检测我的应用程序第一次运行 如何访问项目文件夹中的文件?如果您指的是visual studio项目文件夹中的项目文件夹,我通常右键单击这些文件并将生成操作设置为“嵌入资源”。在运行时,您可以从嵌入式资源中读取数据,如下所示: // The resource name will correspond to the namespace and path in the

当我的应用程序第一次在windows phone上启动时,我想从项目文件夹中获取一些文件(xml/图像),并将它们写入隔离存储

我如何检测我的应用程序第一次运行


如何访问项目文件夹中的文件?

如果您指的是visual studio项目文件夹中的项目文件夹,我通常右键单击这些文件并将生成操作设置为“嵌入资源”。在运行时,您可以从嵌入式资源中读取数据,如下所示:

// The resource name will correspond to the namespace and path in the file system.
// Have a look at the resources collection in the debugger to figure out the name.
string resourcePath = "assembly namespace" + "path inside project";
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resources = assembly .GetManifestResourceNames();
List<string> files = new List<string>();

if (resource.StartsWith(resourcePath))
{
    StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(resource), Encoding.Default);
    files.Add(reader.ReadToEnd());
}

下面是从VisualStudio项目中读取文件的另一种方法。下面显示了如何读取txt文件,但也可以用于其他文件。这里,该文件与.xaml.cs文件位于同一目录中

var res = App.GetResourceStream(new Uri("test.txt", UriKind.Relative));
var txt = new StreamReader(res.Stream).ReadToEnd();

确保您的文件标记为内容。

谢谢Alex,解释得很好,是否可以将文件更新/删除/添加到项目文件夹?@Ishti否,无法添加、编辑或删除分布在XAP中或嵌入程序集中的文件(如本例所示)。不过,您可以使用IsolatedStorage中的文件执行这些操作。(请参阅)除非您有很好的理由不这样做,否则您最好使用
资源
,而不是像Vivek建议的那样使用
嵌入式资源
var res = App.GetResourceStream(new Uri("test.txt", UriKind.Relative));
var txt = new StreamReader(res.Stream).ReadToEnd();