I\'m using a winforms webbrowser control to display some content in a windows forms app. I\'m using the DocumentText property to write the generated HTML. That part is wor
I got it figured out.
I just pass the complete resolved url of the exe directory to the XSL transform that contains the HTML output with image tags:
XsltArgumentList lstArgs = new XsltArgumentList();
lstArgs.AddParam("absoluteRoot", string.Empty, Path.GetFullPath("."));
Then I just prefixed all the images with the parameter value:
<img src="{$absoluteRoot}/Images/SilkIcons/comment_add.gif" align="middle" border="0" />
Ken's code was missing a few things that it needed to work. I've revised it, and created a new method that should automate things a little.
Just call the static method as so:
html = HtmlFormatter.ReplaceImagePathAuto(html);
and all links in the html that match file://ApplicationPath/ will be swapped with the current working directory. If you want to specify an alternate location, the original static method is included (plus the bits it was missing).
public class HtmlFormatter
{
public static readonly string FILE_URL_PREFIX = "file://";
public static readonly string PATH_SEPARATOR = "/";
public static String ReplaceImagePath(String html, String path)
{
return html.Replace("file://ApplicationPath/", path);
}
/// <summary>
/// Replaces URLs matching file://ApplicationPath/... with Executable Path
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static String ReplaceImagePathAuto(String html)
{
String executableName = System.Windows.Forms.Application.ExecutablePath;
System.IO.FileInfo executableFileInfo = new System.IO.FileInfo(executableName);
String executableDirectoryName = executableFileInfo.DirectoryName;
String replaceWith = HtmlFormatter.FILE_URL_PREFIX
+ executableDirectoryName
+ HtmlFormatter.PATH_SEPARATOR;
return ReplaceImagePath(html, replaceWith);
}
}
Here's what we do, although I should mention that we use a custom web browser to remove such things as the ability to right-click and see the good old IE context menu:
public class HtmlFormatter
{
/// <summary>
/// Indicator that this is a URI referencing the local
/// file path.
/// </summary>
public static readonly string FILE_URL_PREFIX =
"file://";
/// <summary>
/// The path separator for HTML paths.
/// </summary>
public const string PATH_SEPARATOR = "/";
}
// We need to add the proper paths to each image source
// designation that match where they are being placed on disk.
String html = HtmlFormatter.ReplaceImagePath(
myHtml,
HtmlFormatter.FILE_URL_PREFIX + ApplicationPath.FullAppPath +
HtmlFormatter.PATH_SEPARATOR);
Basically, you need to have an image path that has a file URI, e.g.
<img src="file://ApplicationPath/images/myImage.gif">
I ended up using something that's basically the same as what Ken suggested. However, instead of manually appending the file prefix, I used the UriBuilder class to build the complete URI with the "file" protocol.
This also solved a subsequent problem when we tested the app in a more realistic location, Program Files. The spaces was encoded, but the OS couldn't deal with the encoded characters when the file was referenced using a standard system path (i.e. "C:\Program%20Files..."). Using the true URI value (file:///C:/Program Files/...) worked.
Alternatively, keep your normal style relative links, drop the HTML transforming code and instead embed a C# web server like this in your exe, then point your WebControl at your internal URL, like localhost:8199/myapp/