问题: - 我想将文件名读入List<>在课堂内。
如何将文件名读入List<>在foreach声明?
如何在List<>中设置get和set语句班上的属性。
对于此操作,使用哪种类型:静态类或普通类?
------修改-------------这就是问题:
抱歉这令人困惑。我想要的是将文件名添加到List<>在课堂里。我希望列表中有一个文件名列表供以后使用。
1)如何将文件名添加到Click事件或页面加载事件中的List g_Photoname中?
protected void Button_Click(object sender,EventArgs e)
{
// - 1-获取Temp目录中的所有文件
var pe = new PicExample(@"c:\temp\");
foreach (string f in pe.FileList)
{
// Question : How do I add the f in the List<> in this class ?
// PhotoNameCollection = f ; ??? Can do?
}
}
2)创建一个静态类来保存List&lt;&gt;中的文件名列表。在课堂上
a)如何为List&lt;&gt;设置get ad set语句在这堂课?
b)List&lt;&gt;在这堂课中使用过吗?
public class PhotoNameCollection
{
private List<string> g_Photoname
public List<string> PhotoName
{
get
{
}
set
{
}
}
}
答案 0 :(得分:1)
这样的事情应该有助于你开始......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var pe = new PicExample(@"c:\temp\");
foreach (var p in pe.FileList)
{
Console.WriteLine(p);
}
Console.ReadLine();
}
}
public class PicExample
{
private List<string> _fileNames = new List<string>();
public PicExample( string directory )
{
var files = Directory.EnumerateFiles(directory);
foreach (var file in files)
{
_fileNames.Add(file);
}
}
public List<string> FileList
{
get { return _fileNames; }
set { _fileNames = value; }
}
}
}