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?
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OBonioCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sbornstein2Author Commented:
there is no File.Move?
0
TechTiger007Commented:
Have you imported System.IO namespace in your project as suggested by OBonio
I am trying to convert his code to vb.net  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
0
sbornstein2Author Commented:
perfect thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.