我试图从服务器读取1000个文本文件。 (使用c#,winforms)
问题是我遇到了2个错误,我不知道它们出现的原因。
错误 1)"}预期" 2)"类型或名称空间定义":
对于错误1,我看到}关闭方法,为什么会出现。同样对于错误2,一切都在命名空间中,为什么它出现?
代码:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ // Error1
public static void ProcessAllFilesUnderDirectory(string topLevelDirectory, string searchMask, Action<string> processFile)
{
var files = Directory.EnumerateFiles(topLevelDirectory, searchMask, SearchOption.AllDirectories);
foreach (var file in files)
processFile(file);
}
private static void ProcessAFile(string fileName)
{
var lines = File.ReadAllLines(fileName);
// perform processing.
}
public static void Main(params string[] args)
{
ProcessAllFilesUnderDirectory(@"\camis01srfs04\DATA\Stats Analysis Project\Sobeys Stats\Atlantic", "*.txt", ProcessAFile);
}
}
}
}//Error 2
答案 0 :(得分:8)
您不能将一种方法放在另一种方法中。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{ // Error1
}
public static void ProcessAllFilesUnderDirectory(string topLevelDirectory, string searchMask, Action<string> processFile)
{
var files = Directory.EnumerateFiles(topLevelDirectory, searchMask, SearchOption.AllDirectories);
foreach (var file in files)
processFile(file);
}
private static void ProcessAFile(string fileName)
{
var lines = File.ReadAllLines(fileName);
// perform processing.
}
public static void Main(params string[] args)
{
ProcessAllFilesUnderDirectory(@"\camis01srfs04\DATA\Stats Analysis Project\Sobeys Stats\Atlantic", "*.txt", ProcessAFile);
}
}
}//Error 2