如何从项目文件夹中读取文件? [英] How to read files from project folders?
问题描述
当我的应用程序第一次在 Windows 手机上启动时,我想从项目文件夹中获取一些文件(xml/images)并将它们写入隔离存储.
When the first time my app starts on a windows phone, I want to get some files(xml/images) from project folders and write them to the isolated storage .
我如何检测我的应用是首次运行?
How do I detect that my app is running for the first time?
如何访问项目文件夹中的文件?
How do I access file in project folders?
推荐答案
如果您的意思是在 Visual Studio 项目中的文件夹中的项目文件夹,我通常会右键单击文件并将构建操作设置为嵌入式资源".在运行时,您可以像这样从嵌入的资源中读取数据:
If you mean project folders as in the folders in your visual studio project, I usually right click on the files and set the build action to 'Embedded Resource'. At runtime, you can read the data from the embedded resource like so:
// 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());
}
要读取图像,您需要这样的东西来读取流:
To read the images you would need something like this to read the stream:
public static byte[] ReadAllBytes(Stream input)
{
byte[] buffer = new byte[32 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
这篇关于如何从项目文件夹中读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!