.NET Framework - Change the working directory of WebBrowser control?

Asked By Sin Jeong-hun on 16-Mar-12 05:00 AM
The situation is like this.
There is an XML in C:\SampleDirectory. I open the XML, modify it, then
transform it using an XSLT. Since I cannot write in C:
\SampleDirectory, I modified the XML on memory and save the
\transformed.html) and open it with a WebBrowser.

The problem is that original XML contains relative paths to images
which are saved in C:\SampleDirectory\Images. The WebBrowser is trying
to show the images in C:\Temp\Images, so images are not shown. Of
course I can manually edit each relative paths ("./") to hard-coded
paths (C:\Temp\Images\", but *if* I can tell the WebBrowser that the
current working is "C:\SampleDirectory" instead of the directory of
the opened HTML, it would be very easy and elegant.

Is this possible? Or should I change the text itself?




Peter Duniho replied to Sin Jeong-hun on 16-Mar-12 10:13 AM
If all you really need is to change the working directory, then you can use
Environment.CurrentDirectory.

However, I'd point out that normally in HTML, paths are not specified using
are found in an "Images" directory contained in the same directory as the
HTML document, then paths of the form "Images/..." are used).

If you have the option of not starting with broken HTML in the first place,
I think that would be best.

Of course, the other thing to keep in mind is that HTML is meant to be
hosted by an HTTP server.  Even if you can change over to true relative
paths, you are still limiting your options.  If this HTML is eventually
going to be hosted by an actual HTTP server, then it may be that for
testing purposes you ought to just have a test HTTP server host the page
and use proper host-relative paths.  In some cases, you will still stick with
the "Images/..." form, but this way you also have the option of using paths
that start with "/" (i.e. are relative to the host, not the page's path).

Pete
Jeong-hun Sin replied to Sin Jeong-hun on 20-Mar-12 09:44 PM
I forgot to post it here but right after I posted this question, I found the answer myself from Google.
The answer was the <base> tag.