c# - How to read text from a text file in the XAP? -
i'm working on out-of-browser silverlight program, , have gotten open local files means of openfiledialog. however, need open file within own xap (no browsing necessary, file open hard-coded). trying use code, it's not working:
using (streamreader reader = new streamreader("default.txt")) { textbox1.text = reader.readtoend(); }
this code throws securityexception
says "file operation not permitted. access path 'default.txt' denied." doing wrong?
your code trying open file called "default.txt" somewhere out in user's file system. don't know, depends on silverlight app's executing from. yes, in general don't have permission go there.
to pull out of xap, need ton construct stream differently. along these lines:
stream s = application.getresourcestream( new uri("/myxap;component/path/to/default.txt", urikind.relative)).stream; streamreader reader = new streamreader(s);
note, means default.txt should set 'resource', not 'embedded resource'. being 'resource' added xap. embedded resource add assembly.
more info: http://nerddawg.blogspot.com/2008/03/silverlight-2-demystifying-uri.html
note: in cases silverlight program has multiple assemblies, check "/myxap" part of uri string references name of assembly containing resource. example if have 2 assemblies "projectname" , "projectname.screens", "projectname.screens" contains resource, use following:
new uri("projectname.screens;component/path/to/default.txt", urikind.relative))
Comments
Post a Comment