views:

355

answers:

3

I'm trying to read in some sample data from an XML file in a Silverlight project, and this line:

using (TextReader reader = new StreamReader(@"C:\Users\mike\Documents\Visual Studio 2008\Projects\test\test\Data\test.xml"))

Throws this exception:

System.MethodAccessException: Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)

--- Inner Exception --- Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)

I'm positive that the path is correct, I copied it out of explorer (pasting the filename at the end) to be sure.

What might cause this problem?

+4  A: 

Silverlight runs in a different security context enforced by browsers. You cannot access the file system at your will. Image if you can, some one can write a silverlight program that deletes users files once the web page is opened.

Codism
Okay. Can you recommend a way to do this, then? I just have a big block of XML in a file that I want to read in, just as a test. I'd hardcode it, but the quotes in the XML terminate the string.
Mike Pateras
Curt Nichols already gave the hint. Yes, OpenFileDialog is the way to go.
Codism
+1  A: 

You can only StreamRead a file from SilverLight if it's on the server.

Mitch Wheat
I read that while trying to figure this out, but isn't my computer the server when I'm just running from Visual Studio?
Mike Pateras
+2  A: 

You'll want to look into OpenFileDialog if you want to open a file in Silverlight.

Curt Nichols