C# 如何在文件列表框中获取文件信息?

C# 如何在文件列表框中获取文件信息?,c#,winforms,C#,Winforms,今天的困境是,我一辈子都想不出一种简单的方法来在列表框中写入文件的数据(大小、日期)。我已经设法创建了一个列表框,每当使用FileSystemWatcher进行更改时,它都会更新,这是另一个用户的一个很好的建议。现在我需要知道这些文件的大小以及它们是何时创建/修改的 先谢谢你 public partial class Form1 : Form { public static string serviceName = "Spooler"; publi

今天的困境是,我一辈子都想不出一种简单的方法来在列表框中写入文件的数据(大小、日期)。我已经设法创建了一个列表框,每当使用FileSystemWatcher进行更改时,它都会更新,这是另一个用户的一个很好的建议。现在我需要知道这些文件的大小以及它们是何时创建/修改的

先谢谢你

    public partial class Form1 : Form
    {
        public static string serviceName = "Spooler";
        public static string directoryLocation = "C:/Windows/System32/spool/PRINTERS/";
        public static int timeoutMilliseconds;
        FileSystemWatcher watcher;

        public Form1()
        {
            //listBox1.

            InitializeComponent();
            RefreshQueue();
            watch();
        }

        private void watch()
        {
            watcher = new FileSystemWatcher();
            watcher.Path = directoryLocation;
            watcher.NotifyFilter = NotifyFilters.LastWrite & NotifyFilters.LastAccess & NotifyFilters.FileName & NotifyFilters.DirectoryName;
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;
            watcher.Renamed += OnChanged;
            watcher.EnableRaisingEvents = true;
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            RefreshQueue();
        }


关于:
listBox1.Items.Add($”{fi.FullName}:{fi.Length})的情况如何;
最好切换到包含各种内容的列的ListView。或者使用新的ToString方法从FileInfo派生一个类,然后将其添加到ListBox。谢谢TaW,我确实怀疑我可能必须这样做。Jeroen的回答也很好,如果你不想切换到ListView,但请记住,对于ListBox,在队形不会对齐。
     public void PrintQueue()
    {
        listBox1.Items.Clear();
        DirectoryInfo di = new DirectoryInfo(directoryLocation);
        foreach (FileInfo fi in di.GetFiles().OrderByDescending(d => d.LastWriteTime))
        {
            listBox1.Items.Add(directoryLocation + fi);
        }
    }

    public void RefreshQueue()
    {
        PrintQueue();
        label1.Text = listBox1.Items.Count.ToString();
    }