How to Load File into WebBrowser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ekimfeenux
    New Member
    • Aug 2008
    • 4

    How to Load File into WebBrowser

    Hi there,

    I currently am trying to get HTML files to load into my WebBrowser control. These HTML files are saved on disk and I have a working OpenFileDialog control, but when I select the file it won't load. When I navigate to the HTML file I want to load and click OK, all it does it is pop up a new dialog box that shows the file address.

    I am new to C# and haven't learned everything yet so what would be the easiest way to get the files to load?

    Thanks for your time and help.

    Michael
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I'm not sure what the problem is...
    Here's something I just tried, and it works:
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
        DialogResult res = openFileDialog1.ShowDialog();
        if (res == DialogResult.OK)
            textBox1.Text = openFileDialog1.FileName;
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        FileInfo fi = new FileInfo(textBox1.Text);
        if (fi.Exists)
        {
            webBrowser1.Navigate(fi.FullName);
        }
    }
    button1 opens the OpenFileDialog, and sets its value to a textbox. But you can store that value anywhere. button2 checks to see if the file selected still exists, and then navigates to that filename. And it works. (FileInfo is in the System.IO namespace)

    You could implement a few more checks, to see if the file is an HTML file, but this basic code here works.

    Comment

    • ekimfeenux
      New Member
      • Aug 2008
      • 4

      #3
      Thanks for the response but what I am trying to do is to have a menu system where the File Open option isn't a button but its located in a menu which for my browser is the usual File->Open File...

      Is there a way to set up the code to work this way?

      Here is what I have currently which lets me navigate to the file, select it but won't load it.

      Code:
      OpenFileDialog fDialog = new OpenFileDialog();
      fDialog.Title = "Open HTML File";
      fDialog.Filter = "HTML files (*.html)|HTM files (*.htm)|All files (*.*)|*.*";
      fDialog.InitialDirectory = @"C:\Documents and Settings\";
      if (fDialog.ShowDialog() == DialogResult.OK)
      {
      
          MessageBox.Show(fDialog.FileName.ToString());
      
      }
      fDialog.CheckFileExists = true;
      
      fDialog.CheckPathExists = true;
      I also tried implementing the above suggestion but I kept receiving errors relating to the System.IO namespace saying that 'Windows' is not part of my webBrowser.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Well, in the code snippet you provided, it seems like you never actually try to navigate the web browser control. Once you've verified that the path exists, you need to do:
        nameOfWebBrowse rControl.Naviga te("urlToFile") ;

        Comment

        • ekimfeenux
          New Member
          • Aug 2008
          • 4

          #5
          Yeah I never had it in there because I didn't know how to do it. I am also getting an assembly reference saying that the type or namespace Navigate does not exist. What namespace is Navigate under or how do I fix this?

          Thank you so much for all the help and sorry for so many questions.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Do you already have a WebBrowser control added to your project?

            Just take the name of that webBrowser, and add .Navigate(urlTo NavigateTo);
            on to the end of it.

            Comment

            • ekimfeenux
              New Member
              • Aug 2008
              • 4

              #7
              Originally posted by insertAlias
              Do you already have a WebBrowser control added to your project?

              Just take the name of that webBrowser, and add .Navigate(urlTo NavigateTo);
              on to the end of it.

              Yes I have a webBrowser control but it keeps saying that .Navigate does not exist within namespace webBrowser.

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                You're going to need to post more of your code...you're obviously using this incorrectly. WebBrowser isn't a namespace, it's a class. And navigate isn't a static method, so you need to be using it from an instance.

                Post all the relevant code so we can see where you've gone wrong.

                Comment

                Working...