Hello,
I am trying to get a subfolder name into a variable. What I do know is the path to where this subfolder will be, but I do not know this subfolder's name. (ex: I know that the subfolder will be in the ABC folder) I need to get this subfolders' name and display it inside a drop down menu. Once I have this subfolder's name, I need to display the files within in a repeater or that sort of server control, where the user can click on each file and open it.
Any help will be greatly appreciated.
Thanks,Z
Zuza WhitePosted Jan 21, 2009, 10:49 AM
Hi,
I used something simnilar but not completely. I created a dataset and then a datatable, and populated it with the file names and paths. Now the reason is that I was hoping to bind it to a repeater. The problem I came accross now, is that although the repeater displays the filenames, they are not links and cannot be opened by a user. how would I allow the user to click on each file and have it open in an ie window?
Below is my code thus far:
//display data files for the most current month
//need to get the path as a string
string[] strFolders = Directory.GetDirectories(Server.MapPath("~/App_Data/ESLM/Current"));
string strSubfolder = strFolders[0];
//this gives the paths to the files in an array
string[] strFiles = Directory.GetFiles(strSubfolder);
//now create a dataset that will hold both path and file name
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Reports");
dt.Columns.Add("Path", Type.GetType("System.String"));
dt.Columns.Add("FileName", Type.GetType("System.String"));
//now iterate to get the info into the datatable
int i = 0;
int j = 0;
foreach (string filename in strFiles)
{
//create a new row of data each loop around, adding to the 2 columns
DataRow dr = dt.NewRow();
dr[i] = filename;
//need to get the actual file name alone
string strName = Path.GetFileName(filename);
//remove the file extension
string[] strWords = strName.Split('.');
strName = strWords[0];
dr[j] = strName;
dt.Rows.Add(dr);
}
rptESLMReports.DataSource = ds;
rptESLMReports.DataMember = "Reports";
rptESLMReports.DataBind();
Thanks a lot.
Rajendra DeopuraPosted Jan 19, 2009, 10:42 PM
Create the object of DirectoryInfo() class and call the method GetFileSystemInfos() to retrieve all the files and directories. GetFileSystemInfos() return an array of FileSystemInfo objects. Iterate through this collection and check whether the current item is a file or directory via FileSystemInfo.Attributes property.
Thanks
Jan MontanoPosted Jan 19, 2009, 9:36 PM
string[] folders = Directory.GetDirectories(@"C:\ABC");
// gets the first subfolder it reads. If there's only one subfolder, then it gets that.
string firstSubFolder = folders[0];
string[] files = Directory.GetFiles(firstSubFolder);
foreach (string filename in files)
{
//
}
Zuza WhitePosted Jan 19, 2009, 3:41 PM
After further research I think I am getting closer. I am able to use DirectoryInfo with a Server.MapPath to pass it a virtual directory path, convert it to a physical directory and then able to view what remaining folders are in it. I think I will now be able to get the folder name from the array created by DirectoryInfo. My code thus far is:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/VirtualPath"));
DirectoryInfo[] diArr = di.GetDirectories();
Now remains to remove what I need from the array.