C3, WPF, Windows 7, Microsoft Ribbon, Visual studio 2010, dotnet 4.5
I would like to write the help files in html and include them inside the .exe file as an resource.
Is there a way of telling the explorer to get the html pages directly from the .exe file ?
If not, I assume the only way to archive it is to create a temporary directory, copy the
files and start the browser pointing to these files.
Is there a magic call that takes a resource directory and unpack/copy it to a directory ?
Like
tempDir = GetTempDirectory();
WPF_MAGIC_RESOURCE_UNPACKER("/help/*", tempDir );
System.Diagnostics.Process.Start(tempDir + Path.DirectorySeparatorChar + "index.html");
Regards Stefan
I agree with #Tigran, these should be stored separate to the binary or even online, but if you feel as though this is the best solution you could probably make use of the Pack URI functionality of WPF.
This is primarily used for images such as icon resources and splash screens, but could be used for any type of content.
You should be able to extract these resources and save them to the file system or to a stream and use as you see fit.
Example: Uri absoluteUri = new Uri("pack://application:,,,/help/contents.htm", UriKind.Absolute);
Related
I have a Windows Forms application for which I have written an extensive manual in Word with helpful pictures. Is there a way to include the .docx in the .exe so that users can click a button in the application to open to manual in Word?
Instead of "attach" I think you mean "embed".
And yes, you can, by storing it as an Embedded Resource.
There are three ways to embed resources in .NET Win32 programs:
As a Win32 PE Resource. This is not easily used from within .NET code as there's no built-in API for them (so you'll need P/Invoke (DllImport)), but means that other programs can acces the resources.
As an Embedded Resource Stream stored directly, this is accessed using the Assembly.GetManifestResourceStream() API in the .NET Framework. This is the fastest way (as it's exposed as a Stream instead of a byte array, so it isn't loaded into memory unnecessarily).
As a Byte[] inside a .NET .resx/.resources resource. This is suboptimal because the file is wrapped in another abstraction and exposed as a Byte[] instead of a Stream, but you can more easily manage the files in Visual Studio.
I recommend the GetManifestResourceStream method, so do this:
In your project, include the Word document in the Project (Solution Explorer > Show All Files > (your doc, right-click) > Include File
Select the file then open Properties and under "Build Action" choose "Embedded Resource"
Note that the project-root relative folder-path will be translated into a dot-separated name prefix for your resource, so if the file is My Project\Documents\Readme.doc then the resource name will be Documents.Readme.doc. You can override this, but you will need to edit your .csproj file directly (using the <LogicalName> element).
Then build, and your *.doc file will be embedded.
In your code, do this:
// use a GUID generator to create a new GUID in a string literal (do not run `Guid.NewGuid()` or `new Guid()` at runtime!) to create a globally unique filename so you don't accidentally overwrite other files
const String _myFileName = "12345.doc";
static void ShellInvokeReadmeDocument() {
String destFileName = Path.Combine( Path.GetTempPath(), _myFileName );
if( !File.Exists( destFileName ) ) {
Assembly assembly = Assembly.GetExecutingAssembly(); // change as appropriate
using( Stream source = assembly.GetManifestResourceStream("Documents.Readme.doc") )
using( Stream destination = File.OpenWrite( destFileName ) ) {
source.CopyTo( destination );
}
}
Process.Start( destFileName );
}
Note that I don't recommend using a .doc or .docx file as it is not guaranteed that the user will have Word installed (unless you know your customers will have it).
Note that WordPad in Windows does not necessarily have the ability to open .doc files! On Windows 7 and later WordPad only opens .docx files and does not support all formatting options, only a small subset, and WordPad on Windows Vista and earlier does not support opening .docx files.
If you want to maximize compatibility, I recommend using .pdf files if you want to preserve printed-page formatting and layout, or an .rtf file.
If this file is meant to constitute a Help or Documentation file, then you should use a .chm file instead, which is structured and fully supported by all Windows versions from Windows 98 onwards - you can also integrate .chm files with WinForms with the "What's this?" button and tooltips (.chm files are not to be confused with .hlp files which are not supported by Windows after Windows Vista).
I am new in monogame.
I loaded an image using Texture2D background;, then go to LoadContent() method and code background = Contect.Load<Texture2D>("background");. I then imported the image in the Content folder.
After that, I compiled it and got this error:
Unable to load background assets
I checked the Image, and it was background.bmp.
I'll keep on looking for a solution for this.
If you have a solution for me please give me a link.
Any help is appreciated. And by the way, I use Visual C# Express 2010.
Basically you have 2 options, you can either add the content to the Content folder directly (if I understand correctly that's what you have done) or you can pre-compile the assets into XNB files first.
If you are using content directly you will need to add the file extension in code like so:
background = Contect.Load<Texture2D>("background.bmp");
And you'll also need to make sure you set the file to Content / Copy if newer in the properties window inside Visual Studio.
As a side note, if you are going to stick with this method I suggest you save your images as PNG files instead of BMP because PNG has lossless compression and supports transparency.
Alternately, you can pre-compile your content files first to store them in a more optimal file format. See https://github.com/mono/MonoGame/wiki/MonoGame-Content-Processing
Right click on the picture, go to Property and you will see. Copy to output. The default is don't copy you need to turn to Copy Always
I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.
Here is sample code
Process.Start(#"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**#"e:\presentation.ppt")**;
How can i replace this path by ppt containing in resources?
Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.
Basically what you do in general is the following:
locate the resource in question and open a resource stream to it.
Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
Eventually remove the temporary file, if any, from step 1.
So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).
string tempFile = Path.GetTempFileName();
using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}
Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).
using (var process = Process.Start(tempFile))
{
process.WaitForExit();
// remove temporary file after use
File.Delete(tempFile);
}
Note: I left out quite some error handling that you might want to add in a real application.
I just started on my second Windows Phone application, and I ran into a problem (ehm..challange). In my app, I have added a folder called "Images", and some sub-folders, like this:
Images -> boys -> graphs
The boys folder contains 1 xml file, and the graphs folder contains hundreds of .gif images. And I'm now wondering how to access these files, how can I build them into the .xap file, what's the recommended way to go?
Should I set the files to "Content" or "Resource", "Copy to output folder"?
And how do I refer to these files, both the xml file in "boys", and the gif images in "graphs"?
A bit confused, and hoping that there is someone who can kick me in the right direction :)
edit, more info
I got it working, loading the xml file with the code below, I haven't tried with the images yet, but is this "the way to do it"?
string xmlBoys = #"images/boys/Names.xml";
Uri uri = new Uri(xmlBoys, UriKind.Relative);
StreamResourceInfo sm = Application.GetResourceStream(uri);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sm.Stream);
XDocument data = XDocument.Load(xr);
If you set the BuildAction on the images to Content you can refer to them by the relative path.
e.g.: new Uri("\Images\boys\graphs\img1.gif", UriKind.Relative);
You don't need to "Copy to output folder" unless you need them there for your own purposes. You probably don't. That option is an artefact of other types of project. Everythign you need to distribute will be bundled in the XAP.
Importantly, however, note that WP7 does not have native support for gif format files. I'd suggest converting these to PNG before trying to use them.
I'm using the flash 10 activex control to load a flash movie into my WinForm. Unfortunately, it only takes a path. What I'm trying to do is load the swf from a stream I can get from an EmbeddedResource so I can embed the swf into my exe such that A) I don't have to worry about paths and B) I don't have my swf quite so exposed - simpler for both usage and deployment.
I know of using f-in-box but I'd like to not add yet another dependency to my project. Is there a way to load the swf into the underlying activex control without having to use a path?
I'm using C# and .Net 3.5sp1.
Is that path always a file path? Or can it be a URL? It should be pretty simple to set up a temp localhost web-server using HttpListener (or similar), that streams back the file in response to an http request.
Another possibility would be named pipes; I wonder if you can convince the control to open a named pipe as though it were a file? (they have a logical file path, after all). I've barely touched these, though - so I can't say 100% whether it would work.
I don't know if flash 10 supports the res protocol to load web content from resource. But I think the web browser control in Windows Forms and WPF support it.
To create a native resource you need an editor of native resource. Visual C++ has one. You can use RC.exe in Windows SDK to compile it to res files before linking in your C# program.
Update:
Looks like the flash plugin for IE does not support the res protocol. embed the .swf file in a resource file and have it load using the movie parameter won't work. Try embed the Flash file in a web archive file (.mht) and add the .mht file to your resource.