c# - 从我的项目中获取 aspx 文件集合

标签 c#

如何将我拥有的所有 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/

相关文章:

c# - 我可以让 Visual Studio 忽略异常的特定*实例*吗?

c# - 安装服务时出现 System.BadImageFormatException

c# - 如何删除 XtraTreeList 中节点图标和节点文本之间的空格

用于网络接口(interface)性能计数器的 C# 实例名称

c# - DotLiquid,一些初学者问题/观察

c# - 日期时间转换 C#

c# - 在 ActionFilter 中检测 ActionResult 类型?

c# - 我怎样才能用 C# 生成真正的(不是伪的)随机数?

c# - 如何以编程方式向 C# Excel vsto 中的单元格添加彩色边框?

c# - 如何在 TcpClient 中正确使用 TPL?