在phone7中打开一个项目文件 [英] Open a project file in phone7
问题描述
你好,我在 VisualStudio 中有一个项目,其中包含根节点下方的文件夹xmlfiles".该文件夹包含我尝试打开的文件mensen.xml"...
Howdy, I have a project in VisualStudio which contains a folder 'xmlfiles' below the root node. This folder contains a file 'mensen.xml' which I try to open ...
但是,当我尝试打开该文件时,调试器会介入并引发异常.
However when I try to open that very file the debugger steps in and throws an exception.
我试过了<代码>if(File.Exists(@"/xmlfiles/mensen.xml") ){布尔存在 = 真;}还有:
I tried it with
if(File.Exists(@"/xmlfiles/mensen.xml") )
{
bool exists = true;
}
as well as:
FileStream fs = File.Open("/xmlfiles/mensen.xml", FileMode.Open);
TextReader textReader = new StreamReader(fs);
kantinen = (meineKantinen)deserializer.Deserialize(textReader);
textReader.Close();
没有任何效果:(.如何在 Phone7 Emulator 中打开本地文件?
Nothin is working :(. How can I open a local file in the Phone7 Emulator?
推荐答案
如果您只是打开它来阅读它,那么您可以执行以下操作(假设您已将文件的 Build Action 设置为 Resource):
If you are just opening it to read it then you can do the following (Assuming you have set the Build Action of the file to Resource):
System.IO.Stream myFileStream = Application.GetResourceStream(new Uri(@"/YOURASSEMBLY;component/xmlfiles/mensen.xml",UriKind.Relative)).Stream;
如果您尝试读/写此文件,则需要将其复制到独立存储.(一定要添加using System.IO.IsolatedStorage
)
If you are attempting to read/write this file then you will need to copy it to Isolated Storage. (Be sure to add using System.IO.IsolatedStorage
)
您可以使用以下方法:
private void CopyFromContentToStorage(String fileName)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
System.IO.Stream src = Application.GetResourceStream(new Uri(@"/YOURASSEMBLY;component/" + fileName,UriKind.Relative)).Stream;
IsolatedStorageFileStream dest = new IsolatedStorageFileStream(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, store);
src.Position = 0;
CopyStream(src, dest);
dest.Flush();
dest.Close();
src.Close();
dest.Dispose();
}
private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)
{
byte[] buffer = new byte[32768];
long TempPos = input.Position;
int readCount;
do
{
readCount = input.Read(buffer, 0, buffer.Length);
if (readCount > 0) { output.Write(buffer, 0, readCount); }
} while (readCount > 0);
input.Position = TempPos;
}
在这两种情况下,请确保文件设置为 Resource 并且您将 YOURASSEMBLY 部件替换为您的程序集名称.
In both cases, be sure the file is set to Resource and you replace the YOURASSEMBLY part with the name of your assembly.
使用上述方法,要访问您的文件,只需执行以下操作:
Using the above methods, to access your file just do this:
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (!store.FileExists(fileName))
{
CopyFromContentToStorage(fileName);
}
store.OpenFile(fileName, System.IO.FileMode.Append);
这篇关于在phone7中打开一个项目文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!