ASP.Net/VB.Net - How do I rename a file in a directory?

I have a directory where I will have one excel file with the XLS extension.  I need to check in the directory that there is only one file and that it is in XLS and then rename it.  I won't know what the actual file name will be.  Any help?
Who is Participating?
OBonioConnect With a Mentor Commented:
Sorry this is C#, but it should be apparent what's needed..

using System.IO;
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[] fis = di.GetFiles("*.xls"); // get all the files in the dir
if(fis.Length==1) // make sure there is only one file
  string filePath = fis[0].FullName;
  File.Move(filePath, newFileNameAndPath);

Open in new window

sbornstein2Author Commented:
there is no File.Move?
Have you imported System.IO namespace in your project as suggested by OBonio
I am trying to convert his code to  below. I guess you can correct any syntax errors

import System.IO
dim di as DirectoryInfo = new DirectoryInfo(dirPath)
dim fis as FileInfo() = di.GetFiles("*.xls") ' get all the files in the dir
if(fis.Length=1) then ' make sure there is only one file
  dim filePath as string = fis(0).FullName
  File.Move(filePath, newFileNameAndPath) ' new file path should point to same directory in order to rename.
end if

Basically when you use File.Move("c:\files\file.xls","c:\files\renamedfile.xls") the application will rename your file from file.xls to renamedfile.xls
sbornstein2Author Commented:
perfect thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.