FileNotFound exception when trying to move a file using File.Move

I have a folder with more than 2000+ MS Excel 2010.XLSX and I need to follow these steps:

  • I need to open every single file

  • Copy the content of cell B4 (+ each file has unique content in cell B4) and

  • add content of cell B4 to original file name

How can I fix this error to successfully rename my tables as 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

      

+3


source to share


2 answers


File.Move(f.FullName, new_filename);

You must use FullName

, you need the full path.

Edit: Not part of your Question, but:



I'm not sure how it works Workbooks.Open

, but if you run into errors, you can close the file before renaming it.

See also Adams' comment.

+2


source


Close the book before moving it. Be sure to release all resources associated with this file. And make sure you use all pathnames



0


source







All Articles