You need to enable JavaScript to run this app. 如何使用.Net获取文件修改日期,实现与现有PowerShell脚本相同的效果
优惠活动
大模型
产品
解决方案
定价
更多

如何使用.Net获取文件修改日期,实现与现有PowerShell脚本相同的效果

PowerShell脚本对应.NET(C#)实现方案

你原有PowerShell脚本的核心逻辑、用到的System.Diagnostics相关API在.NET中可以直接复用,获取文件修改日期可以搭配System.IO命名空间下的FileInfo类实现,完整方案如下:

实现逻辑对应说明

你现有脚本的核心流程为:读取POS设备列表→检测设备在线状态→拉取在线设备指定路径下的exe文件版本信息→记录失败设备→导出结果到CSV,对应的C#实现代码完全对齐该逻辑,同时补充了文件修改日期获取能力:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;

namespace FileInfoCollector
{
    class Program
    {
        static void Main(string[] args)
        {
            // 基础路径配置,和原PowerShell变量对齐
            string posListPath = Path.Combine(AppContext.BaseDirectory, "POS_BoxServiceList.txt");
            string failureListPath = Path.Combine(AppContext.BaseDirectory, "FailureList.txt");
            string resultCsvPath = Path.Combine(AppContext.BaseDirectory, "FileInfoResults.csv");
            int successCount = 0;
            int failureCount = 0;
            List<FileInfoModel> fileInfoList = new List<FileInfoModel>();

            // 初始化失败列表逻辑对齐原脚本
            if (!File.Exists(failureListPath))
            {
                File.WriteAllText(failureListPath, "No failures.\r\n");
            }
            else
            {
                if (File.ReadAllText(failureListPath).Contains("No failures."))
                {
                    File.Delete(failureListPath);
                    File.Create(failureListPath).Dispose();
                }
            }

            // 读取POS设备列表
            string[] posList = File.ReadAllLines(posListPath);
            foreach (string pos in posList)
            {
                if (string.IsNullOrWhiteSpace(pos)) continue;

                // 设备在线检测,等价于原脚本Test-Connection
                using Ping ping = new Ping();
                try
                {
                    PingReply reply = ping.Send(pos, 1000);
                    if (reply.Status != IPStatus.Success)
                    {
                        throw new Exception("设备不可达");
                    }
                }
                catch
                {
                    Console.WriteLine($"Failed to connect to {pos}");
                    failureCount++;
                    File.AppendAllText(failureListPath, $"{pos}\r\n");
                    continue;
                }

                successCount++;
                // 拼接远程共享路径
                string remoteFilePath = $"\\\\{pos}\\c$\\BoxUploadService\\BoxUploadService.exe";
                if (!File.Exists(remoteFilePath)) continue;

                // 获取文件版本、修改日期信息
                FileInfo fileInfo = new FileInfo(remoteFilePath);
                FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(remoteFilePath);

                // 组装结果对象
                fileInfoList.Add(new FileInfoModel
                {
                    ComputerName = pos,
                    FileName = fileInfo.Name,
                    ProductVersion = versionInfo.ProductVersion,
                    FileVersion = versionInfo.FileVersion,
                    LastModifyTime = fileInfo.LastWriteTime // 新增文件修改日期字段
                });
            }

            // 控制台输出结果
            foreach (var info in fileInfoList)
            {
                Console.WriteLine($"设备:{info.ComputerName},文件名:{info.FileName},产品版本:{info.ProductVersion},文件版本:{info.FileVersion},修改时间:{info.LastModifyTime:yyyy-MM-dd HH:mm:ss}");
            }

            // 导出CSV文件,等价于原脚本Export-Csv
            StringBuilder csvSb = new StringBuilder();
            csvSb.AppendLine("ComputerName,FileName,ProductVersion,FileVersion,LastModifyTime");
            foreach (var info in fileInfoList)
            {
                csvSb.AppendLine($"\"{info.ComputerName}\",\"{info.FileName}\",\"{info.ProductVersion}\",\"{info.FileVersion}\",\"{info.LastModifyTime:yyyy-MM-dd HH:mm:ss}\"");
            }
            File.WriteAllText(resultCsvPath, csvSb.ToString(), Encoding.UTF8);
        }
    }

    // 结果数据模型
    public class FileInfoModel
    {
        public string ComputerName { get; set; }
        public string FileName { get; set; }
        public string ProductVersion { get; set; }
        public string FileVersion { get; set; }
        public DateTime LastModifyTime { get; set; }
    }
}

关键API说明

  • 原PowerShell中调用的[System.Diagnostics.FileVersionInfo]::GetVersionInfo和.NET中使用的是完全相同的底层API,调用逻辑无需调整,直接读取对应版本属性即可
  • 文件修改日期通过FileInfo.LastWriteTime属性获取,如需UTC时间可替换为LastWriteTimeUtc
  • 无需引入第三方依赖,所有功能均基于.NET基础类库实现

内容的提问来源于stack exchange,提问作者Fcarias38

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 22:57:04