How can I read all my aspx file names that I have into my project? I need to get the collection of my aspx files.
I want to add them into a DropDownList.
Like
foreach(ASPX file in myProject.aspx.collections) {
dropdownlist1.Items.Add(file.name);//Name could be: Default.aspx
}
Thanks in advance.
Edit: Thank you friends, really all your answers were correct. Finally I did it as next:
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);
}
With reflection, this should work:
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");
Try this:
var dirPath = Server.MapPath("~/");
var ext = new List<string> {".aspx"};
var fileList = Directory.GetFiles(dirPath, "*.*", SearchOptions.AllDirectories)
.Where(s => ext.Any(ex => s.EndsWith(ex));
dropdownlist1.DataSource = fileList;
dropdownlist1.DataBind();
For only file names do this
foreach(string file in fileList)
dropdownlist1.Items.Add(Path.GetFileName(file));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With