欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-14 16:11:00
1.首先,需要指定获取的文件夹,以及获取文件的文件名; 文件夹:strLocalPath = System.Windows.Forms.Application.StartupPath + "\\ExcelTemplate\\"; 文件名:temp.xlsx 2.代码: ExecutionResult ......

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;