What is C# equivalent of VB.Net Rename function?

B

Ben Wan

VB.Net Rename function allow a file to be renamed or moved. How can I do
something similar in C# given that the rename function is not available in
C#?
 
D

Doknjas

Actually, the Rename function is available if you're willing to use
the Microsoft.VisualBasic namespace, but assuming you're not the true
.NET approach is:

System.IO.FileInfo test = new System.IO.FileInfo(sFileName);
//rename:
test.Name = sNewName;
//move:
test.MoveTo(sDestinationFileName);
 
B

Ben Wan

I assume using Microsoft.VisualBasic namespace makes VB functions available
in C#. How is that done?
 
J

Jon Skeet [C# MVP]

Ben Wan said:
I assume using Microsoft.VisualBasic namespace makes VB functions available
in C#. How is that done?

There's an assembly which basically mimics a lot of the VB
functionality. I would recommend against using it without making sure
that a "normal" .NET way of doing what you want isn't available first
though. People reading your C# code shouldn't have to be familiar with
VB, and they're much more likely to know about File.Move than the VB
Rename function.
 
D

Doknjas

Just use it like any other namespace in .NET -
"Microsoft.VisualBasic.Strings". But I agree with Jon Skeet - the
.net version is so simple that there's no reason to use the older VB
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top