Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

How to Load all the files from my Desktop.
i.e. I have to show my all files in the desktop by using any keyword in the select query
Posted

Hi Please update path as per your requirement


C#
private static string UPLOADFOLDER = "Uploads";
public void LoadUploadedFiles(ref GridView gv)
    {
        DataTable dtFiles = GetFilesInDirectory(HttpContext.Current.Server.MapPath(UPLOADFOLDER));
        gv.DataSource = dtFiles;
        gv.DataBind();
        if (dtFiles != null && dtFiles.Rows.Count > 0)
        {
            double totalSize = Convert.ToDouble(dtFiles.Compute("SUM(Size)", ""));
            if (totalSize > 0) lblTotalSize.Text = CalculateFileSize(totalSize);
        }
    }


 public DataTable GetFilesInDirectory(string sourcePath)
    {
        System.Data.DataTable dtFiles = new System.Data.DataTable();
        if ((Directory.Exists(sourcePath)))
        {
            dtFiles.Columns.Add(new System.Data.DataColumn("Name"));
            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 files in dir.GetFiles("*.*"))
            {
                System.Data.DataRow drFile = dtFiles.NewRow();
                drFile["Name"] = files.Name;
                drFile["Size"] = files.Length;
                drFile["ConvertedSize"] = CalculateFileSize(files.Length);
                dtFiles.Rows.Add(drFile);
            }
        }
        return dtFiles;
    }



Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
Select queries are used to query a database - not the desktop.

Have you got a list of all the desktop files in a collection already?
You can use LINQ queries to filter out the collection then.
 
Share this answer
 
use the system.io.directory.getfiles method for get the files list in the specified directory..,

go through the below link in Google., there are so many examples..,

Get File List.. [^]

you can't get file list in desktop using select query...
 
Share this answer
 
Hello,
If you want to get all the filelist from Desktop then It's not feasible with SQL server, It doesn't store your M/C file metadata.

Yes, But you could accomplish this using LINQ on FileInfo

i.e
C#
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\Documents and Settings\hiren.solanki\Desktop");
        IEnumerable<System.IO.FileInfo> files = dir.GetFiles();
        var FileCollection = from file in files
                             select new
                             {
                                 FileFullName = file.Name,
                             };
        foreach (var file in FileCollection)
        {
            Response.Write(file.FileFullName);
            Response.Write("<br/>");
        }


Replace your desktop path inside DirectoryInfo constructer.
Please vote and Accept Answer if it Helped.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900