Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming dirs in C#

Status
Not open for further replies.

palovidiu

Programmer
Nov 30, 2005
14
RO

I am trying to rename a directory from C# , and I can not
find an elegant solution.

Many recommand MoveTo, Move, which seem to me .. more
like an overkill solution.(also i can't seems to shake
rights access violation exceptions beeing throwen)

On the net there are also solutions running batch files
and also , in VB.Net it seems there is FileSystem.Rename
but only in VB.Net.

has anyone found a better solution ?
 
Look in the System.IO namespace.

You will find DirectoryInfo which should allow you to do that type of thing.

 
According to MSDN, you can access VB only classes from C# using the Microsoft.VisualBasic namespace. However I tried it and it did not work. I did not see a FileSystem class in the namespace, or many of the classes MSDN claims is available in this namespace.

That probably would have been a highly ineficient way to go about it anyways.

In C# I believe renaming a directory is the same as moving it, so try this:

Code:
using System.IO;

Directory.Move(strOldDirPath,strNewDirPath);
 
As I wrote before , I tried Directory.Move, and DirectoryInfo.MoveTo , both throw a rights violation exception ..., I tried to remove eventualy present
rights on the directory, like readonly .. which could have
been responsible for that .. but the same.

thank you anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top