Renaming a directory

J

Janiek Buysrogge

Hello,

Maybe this has been covered in the past in this group, but I wasn't
able to find any references. If it has been covered, please provide me
with a link of some sort.

I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

This is the code::

string dir = "C:\TestDir\Dirname1";
DirectoryInfo di = new DirectoryInfo(dir);
string newDirName = di.Name.ToLower();
string newPath = "C:\\TestDir\\" + newDirName;

//di.MoveTo(newPath);
Directory.Move(di.FullName, newPath);


This throws the exception:

Could not rename dir: C:\TestDir\Dirname1 to: C:\TestDir\dirname1
because of the following reason:
Source and destination path must be different.


I'm also looking at P/Invoke with the functions in shell32 to to it,
but that seems overkill...


Any idea's ?

Thanks in advance,

JB
 
B

Bobbo

Janiek said:
I'm trying to rename a directory, eg.: C:\TestDir\Dirname1 to
C:\TestDir\dirname1, as you can see, this is only changed to
lowercase.

Windows path names are case insensitive, which you can test in Windows
Explorer by trying to create two folders; one with a lower case initial
letter and one upper.

In which case [excuse the pun] you have four options:
1. Use Linux or a Mac
2. Change whatever creates the folder in the first place, to
decapitalise the name
3. Use an intermediate name; i.e. change 'Dirname1' to 'temp' then to
'dirname1'.
4. Another option that somebody else will point out which is much
neater than my option 3.
 
A

adi

Try this:

1. Rename your directory to C:\TestDir\Dirname1_temp (make sure you
replace the temp portion with a string that will make your new path
unique
2. Create C:\TestDir\dirname1
3. Copy everything from C:\TestDir\Dirname1_temp
4. Delete C:\TestDir\Dirname1_temp

Pro's: will do the job
Con's:
- extra disk space used (at one moment you have 2 copies of
C:\TestDir\Dirname1)
- extra coding
- operation may take some time if C:\TestDir\Dirname1 contains a lot of
files/data


Janiek Buysrogge a scris:
 
K

Kevin Spencer

Copy the contents of the Directory to another temporary directory. Delete
the first directory, and create the new one with the lower-case name. Copy
the contents of the temp directory back to the new directory and delete the
temp directory.

--
HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Never trust a dunderhead with a blunderbuss.
 
S

Samuel R. Neff

To change case use two separate Move operations as follows:

Directory.CreateDirectory(@"C:\Temp\Dir1");


Directory.Move(@"C:\Temp\Dir1", @"C:\Temp\dir1_temp");
Directory.Move(@"C:\Temp\dir1_temp", @"C:\Temp\dir1");


There's no need to copy the contents of the directory to a different
directory, it'd be a waiste fo space. Make sure the temporary
directory doesn't exist (if it does use a different temp name).

HTH,

Sam
 

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