C#获取指定目录下的指定文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#获取指定目录下的指定文件

農碼一生   2022-05-28 我要评论

1.首先,需要指定获取的文件夹,以及获取文件的文件名;

文件夹:strLocalPath = System.Windows.Forms.Application.StartupPath + "\\ExcelTemplate\\";

文件名:temp.xlsx

2.代码:

ExecutionResult result = new ExecutionResult();
 result.Status = true;
 string strLocalPath = System.Windows.Forms.Application.StartupPath + "\\ExcelTemplate\\";
 if (Directory.Exists(strLocalPath))
 {
 foreach (string GCFile in Directory.GetFiles(strLocalPath, "temp.xlsx", SearchOption.TopDirectoryOnly))
    {
   string fileName = Path.GetFileName(GCFile);
   string filename = Path.GetFileName(GCFile).Replace(".xlsx", "") + DateTime.Now.ToString("_yyyyMMdd") + ".xlsx";
        try
         {
        File.Copy(strLocalPath + fileName, strLocalPath + "Temp\\" + filename, true);
        result = GetA31DayData(strLocalPath + "Temp\\" + filename);
         }
      catch (Exception ex)
          {
         result.Status = false;
         result.Message = "copy file to destination file exception. Msg:" + ex.Message;
         result.Anything = ex.Message;
   if (mesLog.IsErrorEnabled)
   {
 mesLog.Error(ex.StackTrace);
    }
        }
           }
     }
  else
 {
 result.Status = false;
 result.Message = "Directory not exists!";
  }
    return result;

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们