public partial class DirectoryGrid : System.Web.UI.UserControl
{
List objFileList;
String path;
protected void Page_Load(object sender, EventArgs e)
{
path = System.Configuration.ConfigurationManager.AppSettings["Path"].ToString();
bindTree(path);
}
private void bindTree(string path)
{
if (!string.IsNullOrEmpty(path))
{
try
{
DirectoryInfo objDirectoryInfo = new DirectoryInfo(path);
RadTreeNode objRadTreeNode = new RadTreeNode(objDirectoryInfo.Name);
objFileList = new List();
objFileList.Clear();
LoadDirectoriesAndFiles(objDirectoryInfo, objRadTreeNode);
rgDirectory.DataSource = objFileList;
}
catch (Exception ex)
{
}
}
}
// Load Directory and file from given Root into Treeview
private void LoadDirectoriesAndFiles(DirectoryInfo dir, RadTreeNode parentNode)
{
foreach (FileInfo f in dir.GetFiles())
{
try
{
FileDetail objFileDetail = new FileDetail() { FileName = f.Name, FileSize = f.Length };
objFileList.Add(objFileDetail);
}
catch (Exception)
{
}
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
try
{
FileDetail objFileDetail = new FileDetail() { FileName = d.Name };
objFileList.Add(objFileDetail);
RadTreeNode dirNode = new RadTreeNode(d.Name);
//LoadDirectoriesAndFiles(d, dirNode);
}
catch (Exception ex)
{
}
}
}
public class FileDetail
{
public string FileName { get; set; }
public long FileSize { get; set; }
}
protected void RadGridDir_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
(sender as RadGrid).DataSource = objFileList;
}
protected void RadGridDir_SortCommand(object sender, GridSortCommandEventArgs e)
{
}
protected void rgDirectory_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
LinkButton lnkbtn = (LinkButton)dataItem.FindControl("lnkView");
Label lblfileSize = (Label)dataItem.FindControl("lblFileSize");
int fileSize = Convert.ToInt32(lblfileSize.Text);
if (fileSize > 0)
{
lnkbtn.Enabled = false;
}
}
}
protected void lnkView_Click1(object sender, EventArgs e)
{
LinkButton button = sender as LinkButton;
string newpath = button.CommandArgument.ToString();
newpath = path + newpath;
bindTree(newpath);
rgDirectory.Rebind();
}
}
}