非静态字段需要对象引用吗? [英] Object Reference is required for the non-static field?

查看:69
本文介绍了非静态字段需要对象引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS 2013 C#中编写了一个简单的程序,用于将桌面上的文件与网络目录中的文件进行比较。



我在文本框中列出本地文件想要在带有复选框的文本列表中列出远程文件。然后在文本列表中的文件旁边打勾,然后我可以单击按钮下载它们。



尝试添加时给出了一个对象引用错误从正则表达式到文本列表的项目。



-----当前的Windows窗体代码=====

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 =====

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
        }
    }
}





----------



是这是正确的方法,或者是一种更简单的方法。这是我的表单应用程序中许多的一部分。



谢谢



----------

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

推荐答案

错误说你必须有一个您正在引用的对象的实例。你试图引用而没有先创建一个你不能做的实例,因为它不是静态的。
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.


这篇关于非静态字段需要对象引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆