C#怎么获取指定目录下指定文件

发布时间:2022-04-14 15:19:09 作者:iii
来源:亿速云 阅读:471

这篇文章主要介绍“C#怎么获取指定目录下指定文件”,在日常操作中,相信很多人在C#怎么获取指定目录下指定文件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么获取指定目录下指定文件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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;

到此,关于“C#怎么获取指定目录下指定文件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. Scala 获取指定目录所有文件
  2. 如何删除php指定目录文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:Windows怎么配置Java环境变量

下一篇:C语言的auto和register关键字怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》