コード例 #1
0
        public DataTable GetImage(string _pathFolder, string searchPattern)
        {
            DirectoryInfo mainDir = new DirectoryInfo(_pathFolder);

            //Get a list of files and folders in this directory
            //FileSystemInfo[] FSInfos = mainDir.GetFileSystemInfos();
            FileSystemInfo[] FSInfos;
            if (searchPattern == null || searchPattern == "")
            {
                FSInfos = mainDir.GetFileSystemInfos();
            }
            else
            {
                FSInfos = mainDir.GetFileSystemInfos("*" + searchPattern + "*");
            }

            DataTable FileSystemInfosTable = new DataTable();
            DataRow   myDataRow;

            FileSystemInfosTable.Columns.Add(new DataColumn("FileName1", Type.GetType("System.String")));      //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileName2", Type.GetType("System.String")));      //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileName3", Type.GetType("System.String")));      //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileName4", Type.GetType("System.String")));      //FileName

            FileSystemInfosTable.Columns.Add(new DataColumn("FileShortName1", Type.GetType("System.String"))); //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileShortName2", Type.GetType("System.String"))); //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileShortName3", Type.GetType("System.String"))); //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileShortName4", Type.GetType("System.String"))); //FileName


            FileSystemInfosTable.Columns.Add(new DataColumn("IsFile1", Type.GetType("System.Boolean")));    //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("IsFile2", Type.GetType("System.Boolean")));    //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("IsFile3", Type.GetType("System.Boolean")));    //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("IsFile4", Type.GetType("System.Boolean")));    //FileName

            FileSystemInfosTable.Columns.Add(new DataColumn("Modified1", Type.GetType("System.DateTime"))); //Last modified date and time
            FileSystemInfosTable.Columns.Add(new DataColumn("Modified2", Type.GetType("System.DateTime"))); //Last modified date and time
            FileSystemInfosTable.Columns.Add(new DataColumn("Modified3", Type.GetType("System.DateTime"))); //Last modified date and time
            FileSystemInfosTable.Columns.Add(new DataColumn("Modified4", Type.GetType("System.DateTime"))); //Last modified date and time

            string filename;
            int    i = 1;

            myDataRow = FileSystemInfosTable.NewRow();
            foreach (FileSystemInfo FSInfo in FSInfos)
            {
                filename = FSInfo.ToString();
                if (filename == "Thumbnails")
                {
                    continue;
                }


                if (FSInfo.ToString().Length < 15)
                {
                    myDataRow["FileName" + i.ToString()]      = FSInfo.ToString();
                    myDataRow["FileShortName" + i.ToString()] = FSInfo.ToString();
                }
                else
                {
                    myDataRow["FileShortName" + i.ToString()] = FSInfo.ToString().Substring(0, 15) + "...";
                    myDataRow["FileName" + i.ToString()]      = FSInfo.ToString();
                }

                // Kiem tra xem Image nay da Thumbnails chua thi Create Image trong Thumbnails

                /*if (!File.Exists(_pathFolder + @"/Thumbnails/" + FSInfo.ToString()) && checkImageExtension(FSInfo.ToString()))
                 * {
                 *  ThumbnailUtilities _upload = new ThumbnailUtilities();
                 *  _upload.CreateThumbnail(150, _upload.CurrentPath, FSInfo.ToString(), @"Images2018/Uploaded/" + SetFolder());
                 *  _upload.CreateChildThumbnail(90, _upload.CurrentPath, FSInfo.ToString(), @"Images2018/Uploaded/" + SetFolder());
                 * }*/

                myDataRow["Modified" + i.ToString()] = FSInfo.LastWriteTime;


                switch (FSInfo.GetType().ToString())
                {
                case "System.IO.DirectoryInfo":
                {
                    myDataRow["IsFile" + i.ToString()] = false;
                    i += 1;
                    if (i % 5 != 0)
                    {
                        //Tiep tuc them anh vao cot tiep theo:
                        continue;
                    }
                    FileSystemInfosTable.Rows.Add(myDataRow);
                    i         = 1;
                    myDataRow = FileSystemInfosTable.NewRow();
                    break;
                }

                case "System.IO.FileInfo":
                {
                    if (checkExtension(FSInfo.Name))
                    {
                        myDataRow["IsFile" + i.ToString()] = true;
                        i += 1;
                        if (i % 5 != 0)
                        {
                            //Tiep tuc them anh vao cot tiep theo:
                            continue;
                        }
                        FileSystemInfosTable.Rows.Add(myDataRow);
                        i         = 1;
                        myDataRow = FileSystemInfosTable.NewRow();
                    }

                    break;
                }
                }
            }

            if ((i % 5 != 0) && myDataRow["FileName1"].ToString() != "")
            {
                FileSystemInfosTable.Rows.Add(myDataRow);
            }

            return(FileSystemInfosTable);
        }
コード例 #2
0
        public DataTable GetListImage(string _pathFolder, string searchPattern)
        {
            DirectoryInfo mainDir = new DirectoryInfo(_pathFolder);

            FileSystemInfo[] FSInfos;
            if (searchPattern == null || searchPattern == "")
            {
                FSInfos = mainDir.GetFileSystemInfos();
            }
            else
            {
                FSInfos = mainDir.GetFileSystemInfos("*" + searchPattern + "*");
            }

            DataTable FileSystemInfosTable = new DataTable();
            DataRow   myDataRow;

            FileSystemInfosTable.Columns.Add(new DataColumn("FileName", Type.GetType("System.String")));        //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("FileShortName", Type.GetType("System.String")));   //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("IsFile", Type.GetType("System.Boolean")));         //FileName
            FileSystemInfosTable.Columns.Add(new DataColumn("Modified", Type.GetType("System.DateTime")));      //Last modified date and time
            FileSystemInfosTable.Columns.Add(new DataColumn("Avatar", Type.GetType("System.String")));          //Last modified date and time
            FileSystemInfosTable.Columns.Add(new DataColumn("Size", Type.GetType("System.String")));            //Size
            FileSystemInfosTable.Columns.Add(new DataColumn("FileName_IsFile", Type.GetType("System.String"))); //Size

            //Modified 23012008
            FileSystemInfosTable.Columns.Add(new DataColumn("FullName", Type.GetType("System.String"))); //FullName
            //Modified 23012008

            string filename;

            myDataRow = FileSystemInfosTable.NewRow();
            foreach (FileSystemInfo FSInfo in FSInfos)
            {
                filename = FSInfo.ToString();
                if (filename == "Thumbnails")
                {
                    continue;
                }


                if (FSInfo.ToString().Length < 50)
                {
                    myDataRow["FileName"]      = FSInfo.ToString();
                    myDataRow["FileShortName"] = FSInfo.ToString();
                }
                else
                {
                    myDataRow["FileShortName"] = FSInfo.ToString().Substring(0, 50) + "...";
                    myDataRow["FileName"]      = FSInfo.ToString();
                }

                // Kiem tra xem Image nay da Thumbnails chua thi Create Image trong Thumbnails

                /*if (!File.Exists(_pathFolder + @"/Thumbnails/" + FSInfo.ToString()) && checkImageExtension(FSInfo.ToString()))
                 * {
                 *  ThumbnailUtilities _upload = new ThumbnailUtilities();
                 *  _upload.CreateThumbnail(150, _upload.CurrentPath, FSInfo.ToString(), @"Images2018/Uploaded/" + SetFolder());
                 *  _upload.CreateChildThumbnail(90, _upload.CurrentPath, FSInfo.ToString(), @"Images2018/Uploaded/" + SetFolder());
                 * }*/

                myDataRow["Modified"] = FSInfo.LastWriteTime;


                switch (FSInfo.GetType().ToString())
                {
                case "System.IO.DirectoryInfo":
                {
                    myDataRow["IsFile"]          = false;
                    myDataRow["Avatar"]          = @"/images/icons/folder.gif";
                    myDataRow["Size"]            = "Dir";
                    myDataRow["FileName_IsFile"] = myDataRow["FileName"] + "," + myDataRow["IsFile"];
                    myDataRow["FullName"]        = "";
                    FileSystemInfosTable.Rows.Add(myDataRow);
                    myDataRow = FileSystemInfosTable.NewRow();
                    break;
                }

                case "System.IO.FileInfo":
                {
                    if (checkExtension(FSInfo.Name))
                    {
                        myDataRow["IsFile"] = true;

                        if (checkImageExtension(FSInfo.ToString()))
                        {
                            myDataRow["Avatar"] = @"/images/icons/document_add.png";
                        }
                        else
                        if (checkFlashExtension(FSInfo.ToString()))
                        {
                            myDataRow["Avatar"] = @"/images/icons/flash.jpg";
                        }
                        else
                        if (checkWindowMediaExtension(FSInfo.ToString()))
                        {
                            myDataRow["Avatar"] = @"/images/icons/windowmedia.jpg";
                        }

                        myDataRow["FileName_IsFile"] = myDataRow["FileName"] + "," + myDataRow["IsFile"];
                        myDataRow["Size"]            = GetFileSize(((FileInfo)FSInfo).Length);

                        string strolder = HttpContext.Current.Request.PhysicalApplicationPath.Replace(@"\", "/");
                        myDataRow["FullName"] = FSInfo.FullName.Replace("\\", "/").Replace(strolder, System.Configuration.ConfigurationSettings.AppSettings["ImageUrl"]);


                        FileSystemInfosTable.Rows.Add(myDataRow);
                        myDataRow = FileSystemInfosTable.NewRow();
                    }

                    break;
                }
                }
            }


            return(FileSystemInfosTable);
        }