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

C# 如何对FileInfo[]中的文件进行排序,然后将硬盘中最后一个Gif文件从FileInfo[]分配给pictureBox1?,c#,.net,winforms,C#,.net,Winforms,在fileinfo中,我看到大约10000个文件gif文件。 它们从0到10000开始,但它真的是这样排序的吗?如果我将显示文件10000,它是真的最后一个文件还是需要一些如何排序文件信息第一 我做了位图bmp=new-Bitmapfileinfo[fileinfo.Length-1].Name;我想在pictureBox1中显示最后一个gif文件,但这一行出现异常: 参数无效 path_exe = Path.GetDirectoryName(Application.LocalUserAppDa

在fileinfo中,我看到大约10000个文件gif文件。 它们从0到10000开始,但它真的是这样排序的吗?如果我将显示文件10000,它是真的最后一个文件还是需要一些如何排序文件信息第一

我做了位图bmp=new-Bitmapfileinfo[fileinfo.Length-1].Name;我想在pictureBox1中显示最后一个gif文件,但这一行出现异常:

参数无效

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;

类似这样的东西应该可以帮助您:

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: 

记住包括使用System.Linq

您想要什么样的排序标准?是否希望按名称、大小、上次修改、其他进行排序?无论哪种方式,我都建议使用System.Linq和dirinfo.EnumerateFiles*.gif;方法由于这些文件我每X秒从一个网站下载一次,那么按日期和时间排序会更好吗?最近的日期和时间应该是最后一个文件,这个文件我也想显示在pictureBox中,第一个文件按日期和时间最早。
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