none
Reading File content in XBAP application

    Question

  • Hi,
     
    How do I read a file content in XBAP application. I want to show the file contents which is in project itself to the rich text box.

    Please provide me a code snippet for it.
    Prabu
    Monday, September 22, 2008 3:26 PM

Answers

  • Hi Prabu,

        There have been some issues lately around reading .rtf content in partial trust.  This may be a bug but I can't find that thread at the moment (forums being strange over here).  Regardless, here's how you would read files in an .xbap.  Since the files could be built into your .exe, loosely included in the application manifest, or simply hosted on your site of origin, I included snippets for all three.

                // IF:  
                // In project itself = compiled into main exe  
                StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/foo.jpg"));  
                // In project itself = included as "loose content"  
                StreamResourceInfo info = Application.GetContentStream(new Uri("foo.jpg", UriKind.Relative));  
                // In project itself = "same directory on the server as my .xbap file"  
                StreamResourceInfo info = Application.GetRemoteStream(new Uri("pack://siteoforigin:,,,/foo.jpg"));  
     
                Stream toRead = info.Stream;  
     
                // Do your regular file stream reading stuff here...  

    HTH,
    Matt
    SDET : Deployment/Hosting
    • Marked as answer by Marco Zhou Friday, September 26, 2008 9:14 AM
    Monday, September 22, 2008 4:50 PM
    Moderator
  • Here is the original discussion over the bug on reading RTF content in partial trust as Matt mentioned above:

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/13d665d1-454e-45f6-b068-4a0d8606e91f


    Thanks
    • Marked as answer by Marco Zhou Friday, September 26, 2008 9:14 AM
    Thursday, September 25, 2008 6:25 AM

All replies

  • Hi Prabu,

        There have been some issues lately around reading .rtf content in partial trust.  This may be a bug but I can't find that thread at the moment (forums being strange over here).  Regardless, here's how you would read files in an .xbap.  Since the files could be built into your .exe, loosely included in the application manifest, or simply hosted on your site of origin, I included snippets for all three.

                // IF:  
                // In project itself = compiled into main exe  
                StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/foo.jpg"));  
                // In project itself = included as "loose content"  
                StreamResourceInfo info = Application.GetContentStream(new Uri("foo.jpg", UriKind.Relative));  
                // In project itself = "same directory on the server as my .xbap file"  
                StreamResourceInfo info = Application.GetRemoteStream(new Uri("pack://siteoforigin:,,,/foo.jpg"));  
     
                Stream toRead = info.Stream;  
     
                // Do your regular file stream reading stuff here...  

    HTH,
    Matt
    SDET : Deployment/Hosting
    • Marked as answer by Marco Zhou Friday, September 26, 2008 9:14 AM
    Monday, September 22, 2008 4:50 PM
    Moderator
  • Here is the original discussion over the bug on reading RTF content in partial trust as Matt mentioned above:

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/13d665d1-454e-45f6-b068-4a0d8606e91f


    Thanks
    • Marked as answer by Marco Zhou Friday, September 26, 2008 9:14 AM
    Thursday, September 25, 2008 6:25 AM
  • Hi,

    Thanks for the code snippet.

    My actual requirement is, I have a created a project which have been running an external XBAP projects and hosting the output page into Frame. In the frame area I have three tabs, one for showing the output page and another two tabs are showing XAML & Code-Behind code in rich text box.

    If you have any alternative way to do this, please let me know.


    Prabu
    Friday, September 26, 2008 9:31 AM
  •  Hi Prabu,
        Unfortunately I have no idea what you're actually asking here nor how it relates to reading file content.  Could you give some more specifics?  Could you rephrase your question in the form "Here's some code snippets for what I'm trying to do, this is what I expect to happen, and this is what IS happening... what do I do?"???

    Thanks,
    Matt
    SDET : Deployment/Hosting
    Friday, September 26, 2008 5:28 PM
    Moderator
  • Hi Matt,

    I have created a Demo XBAP application with 10 XAML pages. Each page will demonstrates some functionality of controls like Integer TextBox [IntTextBox.xaml], Chart [chart.xaml], Diagam [diagram.xaml], Edit, Grid, etc., All these pages are hosted in
    MainPage.xaml based on the selection from treeview which is showing on left side of my application.
     
    Consider I want to show the chart page [chart.xaml] and I am slecting the chart option from treeview. From the display [Right side of the application], I have three tabs one will show the chart.xaml page [I am using Frame to host the page], other two tabs having RichTextBox which is for showing XAML code & C# codes of chart.

    Now, my problem is, how do I access the
    chart.xaml & chart.xaml.cs files from the project since there is no option to read files from XBAP sample using Uri path. Here is the code snippet which I am using in WPF windows application.

    string text = File.ReadAllText(this.util.SampleCSFilePath);
    CSharpCode = new SyntaxRichTextBoxCS(this.util).FormatCode(text);
    richTextBox_CS.Document.Blocks.Add(CSharpCode);
    richTextBox_CS.InvalidateVisual();

    string text = File.ReadAllText(this.util.SampleXAMLFilePath);
    XamlText = SyntaxRichTextBoxXAML.ColorXAML(text);
    richTextBox_XAML.Document.Blocks.Add(XamlText);
    richTextBox_XAML.InvalidateVisual();

     


    Prabu
    • Edited by P. Prabu Monday, September 29, 2008 9:37 AM
    Monday, September 29, 2008 9:37 AM
  • Matt,

    Your second example is not working for my deployed XBAP.  I have a file (MyData.xml) in my XBAP project (at the top level) that is marked as Content as its build property and (copy always).  I am therefore retrieving it with the following code that follows your second snippet (and the documentation on pack uri's in MSDN)

    Uri uri = new Uri("MyData.xml", UriKind.Relative); 
        //also tried it with new Uri("/MyData.xml", UriKind.Relative);
    StreamResourceInfo info = Application
    .GetContentStream(uri);
    XmlDocument doc = new XmlDocument
    ();
    doc.Load(info.Stream);


    Either of these snippets work fine when launching it from VS2008 but when I publish it to IIS and access it from a browser launch I fail to find the file.

    When I look in the ClickOnce deployment cache it is not being deployed with the rest of the dll's but is being deployed to a separate "Data" path in the ClickOnce cache.  If I manually move the file to where the exe is in the ClickOnce cache the above code does in fact find it.

    The XBAP is a full trust app and I am at VS2008 SP1 in regards to tooling. 

    Anyone else seeing this behavior or am I overlooking something simple?

    --Keith

    Monday, November 03, 2008 4:37 PM
  • I am also facing the same issue. I am not able to see the resource and content files where the EXEs are. so after publishing i am getting  the error.I am using some images as content and resourcedictonary(xaml) as resource.

    Thanks
    Friday, November 07, 2008 10:41 AM
  • The .xml issue sounds like a ClickOnce behavior... I've used XML in my apps before but usually hosted on the site of origin, not as content.  I'll forward this thread to some ClickOnce folks to see if they know about this (i.e. if it's actually a "feature" :))

    -Matt
    SDET : Deployment/Hosting
    Friday, November 07, 2008 11:11 PM
    Moderator
  • Hi Keith,

    Please do mark XML file as Resource in it's properties and then try the following code. It was working fine in my project.

    StreamResourceInfo xmlContent = Application.GetResourceStream(new Uri(@"pack://application:,,,/Samples.xml", UriKind.RelativeOrAbsolute));
    XmlDocument doc = new XmlDocument
    ();
    StreamReader reader = new StreamReader
    (xmlContent.Stream);
    doc.LoadXml(reader.ReadToEnd());


    Prabu
    Tuesday, November 11, 2008 3:40 AM
  • Prabu,
        Your comment is correct but the tricky part here is loading .xml as loose content from a ClickOnce app, since these files get special treatment.  Per Liang Zhou from the ClickOnce development team, the workaround is to mark the .xml file as data file. You cannot easily get the path to the application, but you can easily get the path to the data folder.

    HTH,
    Matt

    SDET : Deployment/Hosting
    Wednesday, November 12, 2008 1:38 AM
    Moderator
  • Hi Matt,

    Thanks for your explanation.

    Could you please elabrate me about the difference between path accession from application and data folders?? 

    The Resources / Content files, both are treated in XBAP application as files which can we access in same way rite [Using Application.GetResourceStream(....) for Resources and Application.GetContentStream(...)??
    Prabu
    Friday, November 14, 2008 3:53 AM
  • Hi,

    I am able to access the Resource file in my Solution as :

    stream = Application.GetResourceStream(new Uri("pack:/Themes/BlueTheme.xaml", UriKind.Relative)).Stream;

    My "BlueTheme.xaml" is in folder "Themes" wchich again is same solution as my project . But if i am using "pack://application:,,,/Themes/BlueTheme.xaml" or "pack://siteoforigin:,,,/Themes/BlueTheme.xaml" its throughing error saying resource not found . I want to know that by using "pack:/Themes/BlueTheme.xaml" how it is accessing the resource ??

    Thanx
    saurabh
    Friday, November 14, 2008 8:52 AM
  •   OK we need to clarify some stuff here:

    "pack:/Themes/" is not an established usage of the pack:// uri syntax and I'd expect that to throw an exception.  I'd guess the reason it works would have to be it throws out the pack:/ part (pack Uris are also technically absolute!) since you can actually resolve that as a relative uri. 

    Regardless,  the APIs generally match what the content is included as (i.e. GetContentStream for Content, GetResourceStream for Resource, GetRemoteStream for site-of-origin (or other in full trust) access.  
     
    Quick rundown of supported pack:// Uris:

    pack://application:,,,/ : For content compiled as type "Resource".   Simply using relative URIs for this will work in most cases as long as you're referencing the current executable.  If it's in another assembly, this is your go-to prefix.

    pack://siteoforigin:,,,/ : Somewhat overloaded.  Either (in a ClickOnce app) means "Absolute URI relative to the base URL of where my deployment manifest (.xbap or .application) comes from", or "Absolute URI relative to the executing folder (for a plain executable).

    For "Content" type in ClickOnce apps (such as .xbaps): Simply use a plain relative Uri.  Don't need a pack:/ URI here.


    Hope this helps,
    Matt
      




    SDET : Deployment/Hosting
    Friday, November 14, 2008 5:31 PM
    Moderator
  • Hi Mat - I picked up the comment of yours that you have used XML content in your XBAP before, and was wondering if you could help me. I have an XBAP that needs to read XML content, and occasionally update it as well. The files are all on the site of origin. Can you please provide a code snippet to do this? I am not sure whether I should use the "Pack..." approach of just a relative filename. I have tried to add the xml files as content files when I build and publish the project, but I get a message that the resource could not be found. I would really appreciate any help.
    Thursday, January 08, 2009 8:39 PM