简体   繁体   English

如何对FileInfo []中的文件进行排序,然后将硬盘中的最后一个Gif文件从FileInfo []分配给pictureBox1?

[英]How do i sort the files in FileInfo[] and then assign the last Gif file from the hard disk from FileInfo[] to pictureBox1?

path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
radarImagesDirectory = Path.Combine(path_exe, radarImagesDirectory);
dirinfo = new DirectoryInfo(radarImagesDirectory);
fileinfo = dirinfo.GetFiles("*.gif");
Bitmap bmp = new Bitmap(fileinfo[fileinfo.Length - 1].Name);
pictureBox1.Image = bmp;

In fileinfo i see about 10000 files gif files. 在fileinfo中,我看到大约10000个文件gif文件。 They start from 0 to 10000 but is it real sorted like that ? 它们从0到10000开始,但是真的是那样排序的吗? If i will display file 10000 is it realy the last file or need some how to sort the fileinfo first ? 如果我将显示文件10000,是最后一个文件,还是需要一些如何首先对fileinfo进行排序?

And i did Bitmap bmp = new Bitmap(fileinfo[fileinfo.Length - 1].Name); 我做了位图bmp = new Bitmap(fileinfo [fileinfo.Length-1] .Name); i wanted to display in the pictureBox1 the last gif file but i'm getting exception on this line: 我想在pictureBox1中显示最后一个gif文件,但是我在这一行遇到异常:

Parameter is not valid 参数无效

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Bitmap..ctor(String filename)
       at Weather_Radar.Form1..ctor() in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Form1.cs:line 47
       at Weather_Radar.Program.Main() in d:\C-Sharp\Weather Radar\Weather Radar\Weather Radar\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Something like this should help you out: 这样的事情应该可以帮助您:

var ordered = dirinfo.EnumerateFiles("*.gif")
                     .OrderByDescending(f => f.LastWriteTime);
FileInfo newest = ordered.FirstOrDefault();
FileInfo oldest = ordered.LastOrDefault();

Image img1 = Image.FromFile(newest.FullName);
// etc etc

(Remembering to include using System.Linq; ) (请记住包括using System.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM