I created a FlowDocument.xaml in my current WPF project. What i want to do is when the user clicks a button the XAML document will be loaded in the code behind, have some data on the document modified, and then print it out. The sticking point is i don't know how load the flow document so that i can modify it.
When I do:
FileStream fs = File.Open("FlowDocument.xaml", FileMode.Open)
It says that it can't find the file. The file is part of the project and I'm guessing it gets packaged with the rest of the project when compiled.
Any help is appreciated
Assuming it is configured to be a Resource, then you can load it like so:
FlowDocument doc= Application.LoadComponent(new Uri("/Path/FlowDocument.xaml", UriKind.RelativeOrAbsolute)) as FlowDocument;
This looks like it might be a path/relative path issue...just for testing purposes, try specifying the entire physical/absolute path in the File.Open statement...
You could also do
string path = Directory.GetCurrentDirectory();
to check to see what the current directory is and then make sure that the file FlowDocument.xaml is in that directory
Related
I am using a custom cursor named hand2.cur in my C#-WPF application. I have added the cursor to a folder named Images which has all the images that I use in my application. However I've realized that I cannot add relative path to use my custom cursor as:
Cursor newCur = new Cursor("Images\\hand2.cur");
window.Cursor = newCur;
So I used this:
string absolute = System.IO.Path.GetFullPath("hand2.cur");
Cursor newCur = new Cursor(absolute);
window.Cursor = newCur;
This tries to find the hand2.cur file in the \bin\Release folder. So I added the file there and I got it working.
But the problem is, if I Publish this application and use it on a different computer, it does not work. Now the problem is with the cursor file path, because if I deploy it after commenting those 3 lines, it works correctly. So what do I do to rectify this problem?
I am using other images from the Image folder in my XAML code and they seem to port fine. But then again my knowledge of WPF is limited so if anyone has any ideas, that would help.
EDIT: I have added my Images folder to the project. I have also set the Build Action of the cursor file hand2.cur to Embedded Resource. However when I use the following two lines, I get an XAMLParseException.
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(new Uri("pack://application:,,,/Slideshow;component/Images/hand2.cur"));
window.Cursor = new System.Windows.Input.Cursor(info.Stream);
The Inner Exception field when I view the details of the error reads: {"Cannot locate resource 'images/hand2.cur'."}
You could make the cursor a resource in your app/assembly and then use GetResourceStream with the pack Uri to the resources location. Pass the Stream of the StreamResourceInfo to the ctor of the Cursor. e.g.
var info = Application.GetResourceStream(new Uri("pack://application:,,,/Images/hand2.cur"));
var cursor = new Cursor(info.Stream);
I've got this working after I added the cursor file hand2.cur to my Resource1.resx resource file. Then I used the following statement in my code:
window.Cursor = new Cursor(new System.IO.MemoryStream(MyNameSpace.Resource1.hand2));
I use Word.Interop to work with Word Document and let user to open a file from hard disk.
Sometimes I get error saying that the file that user has chosen is readonly.
How can I check if a file is readonly or not?
Are you sure you are actually talking about the File attribute (that can be set via the Windows file properties dialog)? If so, you can use FileInfo.IsReadOnly:
FileInfo fileInfo = new FileInfo(#"path\to\file");
if (fileInfo.IsReadOnly)
{
// do something
}
otherwise, refer to this answer if another process is using the file.
I am experimenting with the Microsoft.Office.Interop.Word namespace to do some manipulation of docx files, specifically programmatically merging them.
I created a simple test harness to try out some of the functionality and I am getting a file not found error despite the file being in the executing directory.
All I am trying to do is merge two docx files, doc1.docx and doc2.docx respectively. Again I am just playing around with this functionality at this point so I created a simple C# console app and have the following:
using Word = Microsoft.Office.Interop.Word;
//and in my method:
object defaultTemplate = #"Normal.dotm";
object missing = System.Type.Missing;
object outputFile = "out.docx";
Word.Application wordApplication = new Word.Application();
//using the default Word template
Word._Document wordDocument = wordApplication.Documents.Add(defaultTemplate);
Word.Selection selection = wordApplication.Selection;
//add files manually. THIS is where things are failing
selection.InsertFile("doc1.docx");
selection.InsertFile("doc2.docx");
I get an exception with the message "This file could not be found." in my first call to InsertFile(). I am certain the files exist in my debug directory, which is where this program is running. MSDN explicitly states that if you don't provide a path with your parameter it uses the current directory. It finds the template Normal.dotm fine. I feel like I am missing something simple here but have been messing with it for over a half hour with no luck. Does anyone have any suggestions on what I should be doing here? Thanks for reading!
Make sure for your path is available. use GetCurrentDirectory() to be sure your current directory is what you want. else use full path.
You also might need to set the default file path.
MyWordApp.Application.Options.set_DefaultFilePath()
My C# program uses a web browser control and I programmatically set its html property by laoding it from a html string variable. This almost works well ,but I noticed it lost the reference to the css file. I think a simplest solution is to make the path of the css file absolute,but I want it remains relative to the C# executable.So let me ask how to let the html reference to the css file in such a context.
Thank you in advance.
Edit: I am sorry . My c# application is a desktop one ,although it uses a web browser control.
Edit: Let me put some code. I first load the document from an html file then store it in a variable then for the 2nd time or later I load the document from the variable.
//first load
web_browser_control.Url = new Uri( dir + #"\HTML\default1.html" );
void wB2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{ html_string = web_browser_control.DocumentText; }
//second load or later
web_browser_control.DocumentText = html_string;
You need to have a look at what path it THINKS its resolving, it would be best if you could please include some code, chances are its not resolving to where you think it is as its executing out of the bin directory (is your path correct relative to the path of the executable and the html),
For example is the html its looking at still where you think it is or is it now in a different directory so the relative paths are now obsolete ?
More code would be good and an indication of the control you are using.
Seems you can change property of css file to be "content" and make property "copy to output directory" - "always", so the wep app dll will be in the same directory with css file.
Or you can use MapPath method.
I have an XML file included as part of my Silverlight 4.0 project that I'd like to access at runtime. I have the file saved in a directory named Resources with the Build Action set to "Content" and the Copy to Output Directory set to "Do not copy". If I decompress the XAP file, I see the XML file in the location I expect it to be, but I'm not sure how to reference it from code. I currently have the following:
Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(#"/AssemblyName;component/Resources/MyFile.xml")
Unfortunately, stream is null after running the code above. In addition to the path mentioned above, I've tried "/Resources/MyFile.xml", "/MyFile.xml" and "MyFile.xml", but they all experience the same behavior.
What is the correct way to access an XML file embedded as a resource in a Silverlight application?
A resource with build action "Content" just gets embedded into the xap file, with the same relative directory structure as the application. It does not get embedded as a resource in the assembly.
When set to build action "Content", you should be able to just load the file using something like (or whatever suits your needs):
XElement.Load(<relative directory>/<file>)
The method you're using currently (using a resource stream) is for embedded resources (which have their build action set to "Resource"). And for those, although I haven't tried yet if your method works, usually you'll get the resources using
Application.GetResourceStream
I have used the code snip below to get access to drawables. Not sure it's completely relevant, but hoping this will give you a hint one way or another ...
Resources res = getResources();
spec = tabHost.newTabSpec("groups").setIndicator("Groups", res.getDrawable(R.drawable.ic_tab_groups)).setContent(intent);
As was mentioned by Willem van Rumpt, "content" resources are not usual resources (they aren't stored in assembly). I've checked out this article and could't found at all that you could reference resource, marked as "content" from other assembly.
So, you have two options:
Define XML as embedded resource
Define XML as resource
In first case stream request looks like:
var a = Assembly.Load("AssemblyName");
var s = a.GetManifestResourceStream(#"DefaultNamespace.Resources.XMLFile2.xml");
In second case:
var a = Assembly.Load("AssemblyName");
var rm = new ResourceManager("AssemblyName.g", a);
using (var set = rm.GetResourceSet(CultureInfo.CurrentCulture, true, true))
{
var ums = (UnmanagedMemoryStream)set.GetObject(#"Resources/XMLFile1.xml", true);
}
Hope this helps.