In D Folder files are stored with the name of course name.
In D folder list of files as follows RM (file)
I have one design page as follows Course name
Dropdownlist (In dropdownlist all course will be displayed)RM(Course)
Then i have one Button Show Button.
When i select the Course (RM). i want to display the RM file content details.
For that how can i do using c#. Display files from folder using c#.
In D folder list of files as follows RM (file)
I have one design page as follows Course name
Dropdownlist (In dropdownlist all course will be displayed)RM(Course)
Then i have one Button Show Button.
When i select the Course (RM). i want to display the RM file content details.
For that how can i do using c#. Display files from folder using c#.
Kunal VaishyaPosted Dec 6, 2014, 1:08 AM
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
//Pass the Grid view for to display files.
DisplayFiles(ref gvFiles);
}
catch (Exception ex)
{
}
}
}
public DataTable GetAllFilesFromDirectory(string sourcePath)
{
System.Data.DataTable dtFiles = new System.Data.DataTable();
try
{
if ((Directory.Exists(sourcePath)))
{
dtFiles.Columns.Add(new System.Data.DataColumn("Name"));
dtFiles.Columns.Add(new System.Data.DataColumn("Date"));
dtFiles.Columns.Add(new System.Data.DataColumn("Size"));
dtFiles.Columns["Size"].DataType = typeof(double);
dtFiles.Columns.Add(new System.Data.DataColumn("ConvertedSize"));
DirectoryInfo dir = new DirectoryInfo(sourcePath);
foreach (FileInfo file in dir.GetFiles("*.*").Where(f => !f.Attributes.HasFlag(FileAttributes.Hidden)).OrderBy(f => f.Name.ToUpper()))
{
System.Data.DataRow drFile = dtFiles.NewRow();
drFile["Name"] = file.Name;
drFile["Date"] = file.CreationTime.ToString("MM/dd/yyyy HH:mm");
drFile["Size"] = file.Length;
drFile["ConvertedSize"] = CalculateFileSize(file.Length);
dtFiles.Rows.Add(drFile);
}
}
}
catch (Exception ex)
{
}
return dtFiles;
}
public void DisplayFiles(ref GridView gv)
{
string PathDir ="Your Path";
string strDirecoryPath = Server.MapPath("~" + PathDir + "/");
try
{
if (Directory.Exists(strDirecoryPath))
{
DataTable dtFiles = GetAllFilesFromDirectory(strDirecoryPath);
if (dtFiles != null && dtFiles.Rows.Count > 0)
{
gvFiles.DataSource = dtFiles;
gvFiles.DataBind();
}
else
{
gvFiles.DataSource = null;
gvFiles.DataBind();
}
}
else
{
gvFiles.DataSource = null;
gvFiles.DataBind();
}
}
catch (Exception ex)
{
}
}