Open project file on phone7

Howdy, I have a project in VisualStudio that contains the "xmlfiles" folder below the root of the node. This folder contains the file "mensen.xml" that I am trying to open ...

However, when I try to open this same file, the debugger logs in and throws an exception.

I tried this with if(File.Exists(@"/xmlfiles/mensen.xml") ) { bool exists = true; } as well as: 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(); 

code>

Nothin works :(. How to open a local file in Phone7 emulator?

+4
source share
2 answers

If you just open it for reading, you can do the following (assuming that you set the file assembly action to a resource):

 System.IO.Stream myFileStream = Application.GetResourceStream(new Uri(@"/YOURASSEMBLY;component/xmlfiles/mensen.xml",UriKind.Relative)).Stream; 

If you are trying to read / write this file, you will need to copy it to isolated storage. (Be sure to add using System.IO.IsolatedStorage )

You can use these methods for this:

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

In both cases, make sure the file is set to Resource, and you will replace the YOURASSEMBLY part with the name of your assembly.

Using the methods above, to access your file, simply do the following:

 IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); if (!store.FileExists(fileName)) { CopyFromContentToStorage(fileName); } store.OpenFile(fileName, System.IO.FileMode.Append); 
+8
source

The emulator does not have access to the PC file system. You must deploy the file to the target (emulator). The easiest way to do this is to mark the file as an embedded resource. Set the Build Action file to "Resource" and then extract it at runtime with code similar to this:

 var res = Application.GetResourceStream(new Uri([nameOfYourFile], UriKind.Relative)) 
0
source

All Articles