繁体   English   中英

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

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

我有一个包含2000多个MS Excel 2010 .XLSX电子表格的文件夹,我需要执行以下操作:

  • 我需要打开每个单独的文件

  • 复制单元格B4的内容(+每个文件在单元格B4上具有唯一内容),和

  • 将单元格B4的内容附加到原始文件名

如何修复此错误,以便我可以成功重命名我的电子表格,如上所述?

//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);

您必须使用FullName ,您需要完整路径。

编辑:不是您的问题的一部分,但是:

我不确定Workbooks.Open是如何Workbooks.Open ,但如果遇到错误,您可能会在重命名之前关闭该文件。

亚当斯也评论道。

在移动之前关闭工作簿。 确保释放与该文件关联的所有资源。 并确保在名称中使用整个路径

暂无
暂无

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

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