current directory of WebBrowser control

T

titan.nyquist

The WebBrowser control won't load a css file written in the same
directory as the program. If I put an absolute path to it, it will
load it. Thus, the current directory of the WebBrowser control isn't
the current directory of the program. What is the current directory?
I don't want to use an absolute path, since its contents are saved to
disk, and that should always load the css file no matter where it and
the css file exists.

Titan
 
N

Nicholas Paldino [.NET/C# MVP]

Titan,

That's not true. You must be specifying something in the style element
incorrectly. The web browser has no concept of a current directory when it
is run. The program might, but it doesn't impact anything that the web
browser does. All relative URLs are resolved in relation to the address of
the document that was loaded.

Can you show the HTML and the CSS page? Something tells me that you
might have specified the CSS file incorrectly in the style tag, or you are
missing something from the style tag.
 
Z

Zytan

Nicholas,

I see the same behaviour. If the css is referenced with an absolute
path, WebBrowser sees it and uses it. If the css is referenced
without a path, WebBrowser doesn't find it. The syntax of the css is
perfect, AFAIK. It's been tested inside WebBrowser and in an external
browser, and it works in both cases (as long as a path is given to
it). So, the css syntax isn't causing the problem.

The problem must be, as you stated, "The web browser has no concept of
a current directory when it is run". So, where does it look when you
ask for a file without a path? There's really no current directory?
There's gotta be something. That's just weird. Is there any way to
inquire what the directory might be?

You said: "All relative URLs are resolved in relation to the address
of the document that was loaded." I've noticed that when I navigate
away from a webpage I made in memory, I cannot return to it (although
I can return to any 'real' webpages I click to, as long as I allow
navigation), so this original webpage of mine doesn't 'really exist'.
So, if it doesn't, then relative paths have nothing to go by. But
again, what does it do? Just assume the file doesn't exist? Seems
strange to me. But, I guess we can't change that.
Can you show the HTML and the CSS page?

CSS:

body
{
color: black;
font-family: georgia;
}

HTML:

<link rel="stylesheet" type="text/css" href="log.css" />
html code here

The only solutions I see:

1. include absolute path to CSS (but then the HTML file written to
disk will not work if the CSS is ever moved).

2. include the CSS inside the HTML itself (but then you cannot change
one CSS file and have all HTML files reformatted).

(I am creating an HTML + CSS, for a logger, and showing it in a
WebBrowser in real-time, but also saving them to disk for later
access)

Zytan
 
N

Nicholas Paldino [.NET/C# MVP]

Zytan,

Files can't not have paths. If you have a relative URL in an HTML page,
and you loaded it from disk, then the web browser will look relative to the
URL that the page was loaded from, i.e. the location on disk. Because of
that, you have to place the CSS file in the same directory (that is how the
relative URL will be resolved).

The only case where this won't work is if you loaded the page from
memory. If you loaded the page from memory. The only way you can do this
with the WebBrowser control is to set the DocumentStream property, or to use
the IPersistMemory COM interface on MSHTML (the Document class).

Is this how you are loading it? Or are you using a file url?
 
Z

Zytan

Files can't not have paths.

Huh? Please elaborate.
If you have a relative URL in an HTML page,
and you loaded it from disk, then the web browser will look relative to the
URL that the page was loaded from, i.e. the location on disk. Because of
that, you have to place the CSS file in the same directory (that is how the
relative URL will be resolved).

Yes, I understand all of this. I presume for the webpage loaded from
memory that there is nothing for the relative path to 'connect' to?
I.e. where is ".\format.css" when the webpage is in memory?
The only case where this won't work is if you loaded the page from
memory. If you loaded the page from memory. The only way you can do this
with the WebBrowser control is to set the DocumentStream property, or to use
the IPersistMemory COM interface on MSHTML (the Document class).
Is this how you are loading it? Or are you using a file url?

I use WebBrowser.Doucment.Write(). The webpage does not exist as a
file. The CSS file does. I assumed that the webpage in memory
'existed' at least in the current directory of the HD. But, I guess
it does not.

I thought of a 3rd solution:

3. Write out the webpage to a file, then load that file into
WebBrowser, then it'll have a current directory. (But then, you need
to save a new file as my logger appends to it, and reload the file,
each time. This is slow.)

I think the best solution is to provide the CSS internally in the HTML
file itself. I'd prefer to 'force' a current HD directory so that the
CSS is separate, but if I cannot do that, then inlining it is best.

Zytan
 
N

Nicholas Paldino [.NET/C# MVP]

Zytan,

I used a double-negative. I was stating that if you were indeed loading
the file from disk, then you would have a URL to figure out relative urls
against. However, since you are using Document.Write, my assumption is that
the base url is "about:blank". You can't resolve relative URLs that way.

I would recommend writing the HTML to a disk file, and then writing the
CSS file in the same directory. This way, you can keep the two separate,
and the HTML will render correctly.
 
Z

Zytan

I used a double-negative. I was stating that if you were indeed loading
the file from disk, then you would have a URL to figure out relative urls
against. However, since you are using Document.Write, my assumption is that
the base url is "about:blank". You can't resolve relative URLs that way.

Right, that matches what I think. Since I do navigate to
"about:blank" to get WebBrowser.Document to not be null. So, any
Write from there is appending to that. If I navigated to a blank HTML
in the current directory, then that would probably also work, since
we'd then have a real current directory.
I would recommend writing the HTML to a disk file, and then writing the
CSS file in the same directory. This way, you can keep the two separate,
and the HTML will render correctly.

The reason I choose against this, which I've already stated, is that
(in my case) the log file is to be displayed in real time. Thus, I
have to write to the file, and reload the entire file, for the
WebBrowser to show properly. Simply doing Write is much faster, since
it just appends, which is exactly what a logger wants. So, my
solution is to write the CSS internally in the HTML. Unfortunately,
this combines form and content, but it's not so bar for just a log
file.

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top