directory renaming in C#

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
imran4sin
Cadet
Posts: 10
Joined: Fri Jan 13, 2006 3:07 am
Location: Auckland-NZ ; Islamabad-Pakistan
Contact:

directory renaming in C#

Post by imran4sin »

Is there any method available to rename a certain directory?

I've looked under Directory and DirectoryInfo but cant find much on renaming the directory.

Moving the directory to another is not an option, constraint is the fize size on remote server + time.
thanks.

PS: havent checked google yet
____
Imran.
<You never know what U can do unless you try>
<work hard, harder untill sweat stings the eye>
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re: directory renaming in C#

Post by LinuxFreaK »

Dear imran4sin,
Salam,
imran4sin wrote:Is there any method available to rename a certain directory?
Might be given fucntion will help you out.

Code: Select all

system(mv dir newdir)
Best Regards.
Farrukh Ahmed
imran4sin
Cadet
Posts: 10
Joined: Fri Jan 13, 2006 3:07 am
Location: Auckland-NZ ; Islamabad-Pakistan
Contact:

Post by imran4sin »

Hi LinuxFreak,
Its on a windows environment.


I could use

Code: Select all

public static void Move ( System.String sourceDirName , System.String destDirName )
    Member of System.IO.Directory

Summary:
Moves a file or a directory and its contents to a new location.  
This should really rename the folder to the new folder, i need to try it .. will let you know.
cheers.
____
Imran.
<You never know what U can do unless you try>
<work hard, harder untill sweat stings the eye>
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: directory renaming in C#

Post by lambda »

imran4sin wrote:Is there any method available to rename a certain directory?

I've looked under Directory and DirectoryInfo but cant find much on renaming the directory.
use the method you found: move. the msdn docs say it renames directories as well as moves them.

Code: Select all

using System;

class MV
{
    static void Main(string[] args)
    {
        if (args.Length < 2) {
            System.Console.WriteLine("mv.exe old-dir new-dir");
            return;
        }
        mv(args[0], args[1]);
    }

    static void mv(string olddir, string newdir)
    {
        try  {
                    System.IO.Directory.Move(olddir, newdir);
                }
        catch (Exception) {
            System.Console.WriteLine("error, couldn't mv.");
        }
    }
}
Moving the directory to another is not an option, constraint is the fize size on remote server + time.
on unix, at least, move renames instead of moving. just like what msdn's docs say.

Code: Select all

fn@r50e:~$ mkdir a a/b
fn@r50e:~$ touch a/file1 a/b/file2
fn@r50e:~$ ls -laRi a
a:
total 12
547466 drwxr-xr-x   3 fn fn 4096 2006-02-05 02:29 .
353410 drwxr-xr-x  24 fn fn 4096 2006-02-05 02:29 ..
547467 drwxr-xr-x   2 fn fn 4096 2006-02-05 02:29 b
547468 -rw-r--r--   1 fn fn    0 2006-02-05 02:29 file1

a/b:
total 8
547467 drwxr-xr-x  2 fn fn 4096 2006-02-05 02:29 .
547466 drwxr-xr-x  3 fn fn 4096 2006-02-05 02:29 ..
547469 -rw-r--r--  1 fn fn    0 2006-02-05 02:29 file2
fn@r50e:~$ ./mv.exe a c
fn@r50e:~$ ls -laRi c
c:
total 12
547466 drwxr-xr-x   3 fn fn 4096 2006-02-05 02:29 .
353410 drwxr-xr-x  24 fn fn 4096 2006-02-05 02:29 ..
547467 drwxr-xr-x   2 fn fn 4096 2006-02-05 02:29 b
547468 -rw-r--r--   1 fn fn    0 2006-02-05 02:29 file1

c/b:
total 8
547467 drwxr-xr-x  2 fn fn 4096 2006-02-05 02:29 .
547466 drwxr-xr-x  3 fn fn 4096 2006-02-05 02:29 ..
547469 -rw-r--r--  1 fn fn    0 2006-02-05 02:29 file2
fn@r50e:~$
imran4sin
Cadet
Posts: 10
Joined: Fri Jan 13, 2006 3:07 am
Location: Auckland-NZ ; Islamabad-Pakistan
Contact:

Post by imran4sin »

yes.. move worked nicely... as expected.

I was making a silly mistake of using the wrong function .. must have done it --late night.

anyhow.. cheers.
____
Imran.
<You never know what U can do unless you try>
<work hard, harder untill sweat stings the eye>
Post Reply