Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
I wrote a simple program in VS 2013 C# to compare files on desktop to files in a web directory.

I am listing the local files in a textbox and want to list the remote files in a textlist with checkboxes. Then place a check next to the files in the textlist so I can then click a button to download them.

It is giving me an Object reference error when trying to add the items from regex to the textlist.

----- Current Windows Forms Code =====
C#
namespace HardwareUtility
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

    public partial class frmFirmware : Form
    {
        public frmFirmware()
        {
            InitializeComponent();
            ftpFirmwareList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); 
        }
        public static string GetDirectoryListingRegexForUrl(string url)
           // get the files in the url directory and list in the textlist
        {
            if (url.Equals("http://website/subfolder/"))
            {
                return "<a href="\".*\"">(?<name>.*)</a>"; // [Ed. colouriser bug due to \""]
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string html = reader.ReadToEnd();
                    Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
                    MatchCollection files = regex.Matches(html);
                    if (files.Count > 0)
                    {
                        foreach (Match files in matches)
                        {
                            webFirmwareList.Items.Add(files.Groups[1]);
                        }
                        throw new NotSupportedException();
                    }
                }
            }
        }  
        private void frmFirmware_Load(object sender, EventArgs e)
        {
            // use to get the application directory           
           string appPath = System.IO.Directory.GetCurrentDirectory(); //Get the application directory
            
            // Combine the appPath the Firmware dir to create location path
            string firmwarePath = System.IO.Path.Combine(appPath, "Firmware\\");

            listBox1.Items.Clear();

            string[] files = Directory.GetFiles(firmwarePath);
            string[] dirs = Directory.GetDirectories(firmwarePath);

            foreach (string file in files)
            {

                listBox1.Items.Add(Path.GetFileNameWithoutExtension(file));
            }
         }
        private void okButton_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void downloadButton_Click(object sender, EventArgs e)
        {
          // code to copy the new files into the local dir and delete the old file
        }
    }
}

[Edit (Matt T Heffron): wrap code in pre tags for formating]
----------

Is this the correct way to do this or is their an easier method. This is one part of many in my forms application.

thanks
Posted
Updated 18-Jul-14 16:07pm
v8
Comments
ZurdoDev 21-Mar-14 16:14pm    
The error says that you have to have an instance for the object you are referencing. You are trying to reference without first having created an instance of it which you can't do because it is not static.
Sergey Alexandrovich Kryukov 21-Mar-14 16:23pm    
Will you please post it as the answer, to close the issue?
I would also add that OP needs to learn basics of OOP, before writing any UI.
—SA
ZurdoDev 21-Mar-14 16:25pm    
True. I was hoping they might point to a line of code but you're right.
Member 10654495 21-Mar-14 16:39pm    
The textlist is created and I can add items in the design mode that show up . I tried to mode the code for the httpwebrequest to a private void buttonclick. but that didn't work either.
This is my first program with C#. what is OP and OPP?

The error happened in the public static string GetDirectoryListingRegexForUrl(string url) when I added the httpWebRequest code to it so it would populate the textlist.
Matt T Heffron 21-Mar-14 16:54pm    
FYI: terminology:
OP = Original Poster (instead of using him/her...) (In this case, that's you!)
OOP = Object-Oriented Programming
UI = User Interface

1 solution

The error says that you have to have an instance for the object you are referencing. You are trying to reference without first having created an instance of it which you can't do because it is not static.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Mar-14 16:28pm    
5ed. If this needs clarification, you or someone else will answer OP's follow-up questions.
OP needs to learn what is the type and what is the instance (object), static vs instance members, and how the class/structure static/instance members are accessed. Those issues lie in the very basics of OOP, event before "real" OOP aspects such as late binding/polymorphism/interfaces...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900