当前位置: 移动技术网 > IT编程>开发语言>c# > C#获取指定目录下的指定文件

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

2020年04月21日  | 移动技术网IT编程  | 我要评论

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;

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网