-4
class testapplication
{
    static void ProcessString(string s)
    {

        {
        Directory.Move("@C:/Users/Public/TestFolder", "@C:/Users/Public/TestFolder1");<1
        Directory.Move("@C:/Users/Public/TestCase", "@C:/Users/Public/TestCase1");<2
        }
    }
}

For a example like this case, it can only run 1 and 2 cannot active.

enter image description here

hkf
  • 4,440
  • 1
  • 30
  • 44
Fabre
  • 253
  • 1
  • 3
  • 8

4 Answers4

0

[Directory.Move Method (String, String)][1] Moves a file or a directory and its contents to a new location.

So at the time of execution of the second command the source becomes empty. if you need to take a copy of testFolder, as testfolder1 and testFolder2 and remove the testfolder, i prefer you to do a copy first and then perform move

try
 { 
  Directory.Copy(@"c:/Users/Public/TestFolder", @"c:/Users/Public/TestFolder1",true);// will take a copy of the directory TestFolder1
  Directory.Move(@"c:/Users/Public/TestCase", @"c:/Users/Public/TestCase1");//will move TestFolder to TestFolder2
 }
catch{//handle exception here}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Your code depicts a move operation which will cause an error in the second line.. a copy operation will be successful :)

Suraj
  • 96
  • 1
  • 2
  • 13
0

I think that should check accessible for source folder before move it.

See check Check if directory is accessible

I just test on my computer. It works fine with following code

if(CanRead(@"C:/Users/hai2/Desktop"))  
   Directory.Move(@"C:/Users/hai2/Desktop", @"C:/Users/hai2/Desktop1");// command 1
if(CanRead(@"C:/Users/hai2/Links"))  
   Directory.Move(@"C:/Users/hai2/Links", @"C:/Users/hai2/Links1");//command 2

Hope it helps

Community
  • 1
  • 1
Lewis Hai
  • 1,114
  • 10
  • 22
0

If you want to execute two operations at the same time, then you can start them in two separate threads. The best way to do this is to use Task functionality.

var move1 = Task.Factory.StartNew(() => Directory.Move("@C:/Users/Public/TestFolder", "@C:/Users/Public/TestFolder1"));
var move2 = Task.Factory.StartNew(() => Directory.Move("@C:/Users/Public/TestCase", "@C:/Users/Public/TestCase1"));

Task.WaitAll(move1, move2); // will wait until work is done

It will make start two asynchronous Directory.Move operations, and will continue only when both of them will be finished.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101