非静态字段、方法或属性“System.Windows.Forms.FileDialog.FileName.get”(CS

本文关键字:FileName FileDialog Forms get CS Windows System 字段 静态 方法 属性 | 更新日期: 2023-09-27 18:33:04

每当我尝试运行它时,我都会收到此错误:非静态字段、方法或属性"System.Windows.Forms.FileDialog.FileName.get"(CS0120)需要对象引用

我对如何解决感到非常困惑,因为我已经看到了其他论坛,但他们与我的问题不同。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Configuration;
namespace Test
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
public partial class MainForm : Form
{
    public MainForm()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();
        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
    }
    void MainFormLoad(object sender, EventArgs e)
    {
        ContextMenu cMenu = new ContextMenu();
        MenuItem one = new MenuItem("test", new EventHandler(TestClick));
        cMenu.MenuItems.Add(one);
        Minecraft.ContextMenu = cMenu;
        //Minecraft.Image = new Bitmap(System.Configuration);
    }
    void newClick(object sender, EventArgs e)
    {
        OpenFileDialog gamedir = new OpenFileDialog();
        if (gamedir.ShowDialog() == DialogResult.OK)
        {
            try 
            {
            }
            catch
            {
            }
        }
    }
    void TestClick(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();
        if (openFile.ShowDialog() == DialogResult.OK)
        {
            Minecraft.Image = new Bitmap(openFile.FileName);
        }
    }
    void Button1Click(object sender, EventArgs e)
    {
        try
        {
        Process.Start(@"D:'Games'Minecraft'Minecraft");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    void Button3Click(object sender, EventArgs e)
    {
    }
    void Button2Click(object sender, EventArgs e)
    {
    }
    void Label1Click(object sender, EventArgs e)
    {
    }
    void Dark_CrusadeClick(object sender, EventArgs e)
    {
        Process.Start(@"D:'Games'Dark crusade'DarkCrusade.exe");
    }

    void Add_GameClick(object sender, EventArgs e)
    {
         int top = 60;
         int left = 405;
         for(int i = 0; i<1; i++)
         {
            OpenFileDialog newgame = new OpenFileDialog();
            string FilePath = OpenFileDialog.FileName;
            if (newgame.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            Button button = new Button();
            button.Top = top;
            button.Left = left;
            button.Height = 265;
            button.Width = 189;
            this.Controls.Add(button);
            top += button.Height + 2;
            }
         }
    }
}

}

非静态字段、方法或属性“System.Windows.Forms.FileDialog.FileName.get”(CS

OpenFileDialog newgame = new OpenFileDialog();
string FilePath = OpenFileDialog.FileName;

访问 FileName 时,您在静态上下文中引用OpenFileDialog;您需要使用 OpenFileDialognewgame实例才能检索 FileName 属性:

OpenFileDialog newgame = new OpenFileDialog();
string FilePath = newgame.FileName;