C#/Windows窗体:错误cs1519

C#/Windows窗体:错误cs1519,c#,windows,forms,C#,Windows,Forms,我正在开发一个图形转换程序,设置一个列表框来读取目录中的文件名。代码是基于我的指导老师的一个例子,所以我认为它可以很好地工作,但它似乎到处都会产生错误。我在谷歌上搜索了“errorCS1519:Invalid token”,“in class,struct,or interface member declaration”,但我发现的似乎不适用。这是: using System; using System.Collections.Generic; using System.ComponentMod

我正在开发一个图形转换程序,设置一个列表框来读取目录中的文件名。代码是基于我的指导老师的一个例子,所以我认为它可以很好地工作,但它似乎到处都会产生错误。我在谷歌上搜索了“errorCS1519:Invalid token”,“in class,struct,or interface member declaration”,但我发现的似乎不适用。这是:

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

namespace Transformer
{
 public partial class Transformer : Form
 {
  /* Initialize parameters */
  private bool drawAxes = true;
  private bool drawGrid = true;

  private List<ObjectSettings> dispObjects = new List<ObjectSettings>();

  /* Initialize form */

  public Transformer()
  {
   InitializeComponent();
  }

  private void Transformer_Load(object sender, EventArgs e)
  {
  }


  /* Populate available objects listbox */

  private int selFile = 0;
  private string currentDir = Directory.GetCurrentDirectory();

  // errors start around here

  private string[] fileEntries = Directory.GetFiles(currentDir+@"\Objects");
  foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
  } // end foreach
 }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
名称空间转换器
{
公共部分类转换器:表单
{
/*初始化参数*/
私有布尔图轴=真;
private bool drawGrid=true;
私有列表dispObjects=新列表();
/*初始化表单*/
公共变压器()
{
初始化组件();
}
私有无效转换器加载(对象发送器、事件参数e)
{
}
/*填充可用对象列表框*/
私有int-selFile=0;
私有字符串currentDir=Directory.GetCurrentDirectory();
//错误从这里开始
私有字符串[]fileEntries=Directory.GetFiles(currentDir+@“\Objects”);
foreach(文件项中的字符串s){
int start=s.LastIndexOf(@“\”);
int end=s.LastIndexOf(@“);
添加(s.Substring(start+1,end-start-1));
}//结束foreach
}
}

这是因为您需要:

 foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
  } 
在函数中

也许你可以这样做:

private void Transformer_Load(object sender, EventArgs e)
  {

 int selFile = 0;
string currentDir = Directory.GetCurrentDirectory();
string[] fileEntries = Directory.GetFiles(currentDir+@"\Objects");
  foreach (string s in fileEntries) {
      int start = s.LastIndexOf(@"\");
      int end = s.LastIndexOf(@".");
      availObjectsListBox.Items.Add(s.Substring(start + 1, end - start - 1));
    } // end foreach
  }

请注意,您将
foreach
回路置于
Transformer\u Load
功能内;当然,您可以将其放在任何其他函数中。请注意,
selFile
currentDir
fileEntries
变量前面没有修饰符(
private

看看错误从哪里开始

在类主体中不能有语句。它需要位于方法/属性/构造函数中