简体   繁体   English

尝试使用File.Move移动文件时出现FileNotFound异常

[英]FileNotFound exception when trying to move a file with File.Move

I have a folder with over 2000+ MS Excel 2010 .XLSX spreadsheets and I need to perform the following actions: 我有一个包含2000多个MS Excel 2010 .XLSX电子表格的文件夹,我需要执行以下操作:

  • I need to open each individual file 我需要打开每个单独的文件

  • Copy the content of cell B4 (+ each file has unique content on cell B4), and 复制单元格B4的内容(+每个文件在单元格B4上具有唯一内容),和

  • append the content of cell B4 to the original file name 将单元格B4的内容附加到原始文件名

How can I fix this error so I can successfully rename my spreadsheets as mentioned above? 如何修复此错误,以便我可以成功重命名我的电子表格,如上所述?

//file rename code
DirectoryInfo d = new DirectoryInfo(@"C:\Torename\");
FileInfo[] infos = d.GetFiles();
foreach (FileInfo f in infos)
{

    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

    Workbook wb = excel.Workbooks.Open(f.FullName);
    Worksheet excelSheet = wb.ActiveSheet;


    //Read the first cell
    string test = excelSheet.Cells[4, 2].Value.ToString();


    File.Move(f.Name, new_filename);

    ***Thank you for your assistance that did the trick

File.Move(f.FullName, new_filename);

You have to use FullName , you need the full path. 您必须使用FullName ,您需要完整路径。

Edit: Not part of your Question, but: 编辑:不是您的问题的一部分,但是:

I'm not sure how Workbooks.Open works, but if you run into errors you might close the file before renaming it. 我不确定Workbooks.Open是如何Workbooks.Open ,但如果遇到错误,您可能会在重命名之前关闭该文件。

Also Adams comment. 亚当斯也评论道。

Close the workbook before moving it. 在移动之前关闭工作簿。 Make sure to release all the resources associated with that file. 确保释放与该文件关联的所有资源。 And make sure you use the whole path in the names 并确保在名称中使用整个路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM