如何将我拥有的所有 aspx 文件名读取到我的项目中?我需要获取我的 aspx 文件的集合。
我想将它们添加到 DropDownList
中。
喜欢
foreach(ASPX file in myProject.aspx.collections) {
dropdownlist1.Items.Add(file.name);//Name could be: Default.aspx
}
提前致谢。
编辑: 谢谢各位 friend ,真的你们的回答都对了。最后我做了下一个:
string sourceDirectory = Server.MapPath("~/");
DirectoryInfo directoryInfo = new DirectoryInfo(sourceDirectory);
var aspxFiles = Directory.EnumerateFiles(sourceDirectory, "*.aspx", SearchOption.TopDirectoryOnly).Select(Path.GetFileName);
foreach (string currentFile in aspxFiles) {
this.dropdownlist1.Items.Add(currentFile);
}
最佳答案
通过反射(reflection),这应该可行:
public static IEnumerable<string> getAspxNames()
{
var pageTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.BaseType == typeof(Page));
return pageTypes.Select(t => t.Name).ToList();
}
// ...
foreach(string pageName in getAspxNames())
dropdownlist1.Items.Add(pageName + ".aspx");
关于c# - 从我的项目中获取 aspx 文件集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455394/