Solved

Check Directory & Read All Files One At A Time In C#

Posted on 2006-11-14
13
245 Views
Last Modified: 2010-04-16
Hey guys,

I am trying to write an application that will do the following:

Check how many files (html) are in a folder
Read those html files
Display them one at a time in axWebBrowser.  

Basically it has to know how many files there are, load the intial one into the web browser I created, and then when the user clicks a button called Next (btnNext) it will load the 2nd html file into the same web browser (user can now see the 2nd one, but can't see the first unless they press Previous[btnPrevious])

At the minute I have this which isnt very good if the filenames/locations are different:
            System.Object nullObject = 0;
            string str = "";
            System.Object nullObjStr = str;
            Cursor.Current = Cursors.WaitCursor;
            axWebBrowser1.Navigate(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\OpenFiles\Testing\bin\Debug\AUPs\test.html", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            Cursor.Current = Cursors.Default;
0
Comment
Question by:angus_young_acdc
  • 7
  • 5
13 Comments
 
LVL 8

Expert Comment

by:redpipe
Comment Utility
I am not familiar with the WebBrowser component but the following method shows a way to get an array of htm/html files in a given folder:

private ArrayList GetWebFiles(string folderName)
{
  DirectoryInfo di = new DirectoryInfo(folderName);
  if (!di.Exists) {
    throw new DirectoryNotFoundException();
  }
  ArrayList result = new ArrayList();
  foreach (FileInfo fi in di.GetFiles("*.htm*")) {
    result.Add(fi.Name); // use fi.FullName if you need complete filereference
  }
  return result;
}
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = d.GetFiles("*.html");
0
 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
Hey guys,

thanks for the replies, though I am not sure how I can use them to display the information?

They are to be shown in a MS Explorer (active x webbrowser) called axWebBrowser.   Using either example how can I load the first HTML file into the browser automatically?   And then how do I change to another HTML file by clicking Next?   The HTML files will have pictures & text.
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
To load the first file you would do this :


int currentpos = 0;

void LoadCurrentFile(){
     axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

}

void Next(){
    this.currentpos++;
    this.LoadCurrentFile();
}

void Previous(){
    this.currentpos--;
    this.LoadCurrentFile();

}
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
You may want to check the logic so that currentpos cannot go beyond the length of the files array or be less than 0.
0
 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
Ok I have done a quick test by putting it in, (i made a new project and put all the code into Form1_Load).   I am getting a good few errors but I would say that solving one would do the trick.  The first error is:

The name 'd' does not exist in the current context

Any idea what I can do to solve this?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
Sorry, bug in my code :

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = d.GetFiles("*.html");

should be

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo("path_goes_here");
FileInfo[] files = directory.GetFiles("*.html");
0
 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
No worries mate, I'm definately stuck though.   I am getting "The name 'files' does not exist in the current context.  I must be putting the code in the wrong place like normal.

This is the code I have got in this little test:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication21
{



    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\AUPTestingPrototype\Testing\bin\Debug\AUPs");
            FileInfo[] files = directory.GetFiles("*.html");
        }
 
        void LoadCurrentFile()
        {
           
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

        }

        void Next()
        {
            this.currentpos++;
            this.LoadCurrentFile();
        }

        void Previous()
        {
            this.currentpos--;
            this.LoadCurrentFile();

        }
    }
}

Am I meant to modify the LoadCurrentFile part?  As pressing '.' doesn't bring up any options for me to pick.   Sorry for the confusion
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 75 total points
Comment Utility
You need to declare the fileInfo[] files at class level :

namespace WindowsApplication21
{



    public partial class Form1 : Form
    {

       FileInfo[] files;
       DirectoryInfo directory


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           directory = new System.IO.DirectoryInfo(@"C:\Documents and Settings\xxx\My Documents\Visual Studio 2005\Projects\AUPTestingPrototype\Testing\bin\Debug\AUPs");
           files = directory.GetFiles("*.html");
        }
 
        void LoadCurrentFile()
        {
           
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);

        }

        void Next()
        {
            this.currentpos++;
            this.LoadCurrentFile();
        }

        void Previous()
        {
            this.currentpos--;
            this.LoadCurrentFile();

        }
    }
}
0
 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
Cheers mate with a little tinkering I have it basically working now, last set of issues if that's ok?

How can I set it so that if there are no HTML files it won't give me an error?  Or that if I have been through all the files it will just look through them again, or the button becomes disabled or something.  
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
OK.

         void LoadCurrentFile()
        {
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            this.backButton.Enabled = (this.currentpos > 0);
            this.nextButton.Enabled = (this.currentpos < (this.files.Length -1));
        }

0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
The above will enable or diable the next and back button depending on what the current position of the file is within th fileinfo array.
You  could also do this :

void LoadCurrentFile()
        {
            if (files.Length == 0){
                 this.backButton.Enabled = false;
                 this.nextButton.Enabled = false;
                 MessageBox.Show(this,"No files found in directory","No files");
                 return;
            }
            axWebBrowser1.Navigate(files[currentpos].FullName, ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
            this.backButton.Enabled = (this.currentpos > 0);
            this.nextButton.Enabled = (this.currentpos < (this.files.Length -1));
        }


0
 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
Cheers dude, all seems to work great now.  As I say I had to modify it a bit (didn't like ref nullObject) but that's down to me being a noob lol.  

Thanks again!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now