?
Solved

How to check if a directory move had been finished

Posted on 2010-11-29
4
Medium Priority
?
589 Views
Last Modified: 2013-12-17
Hi,

at a webside a user can create a temporary ID for a new document and file uploads.
When the user press Save at the app I do a callback to move the tempoary folder and the uploaded files to the right folder.
string tpath = GetNewId();
string spath = Session["TempId"];
Directory.Move(spath, tpath);

The matter is that the new directory (tpath) will be scanned some commands later and very often Directory.Move hasn't finished than and I get a exception.
So what I need is to wait until Directory.Move has finished all activity or a simular function that can do so.

How can I solve this problem?

Thanks

Andre
0
Comment
Question by:andre72
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
abdkhlaif earned 1000 total points
ID: 34230664

DirectoryInfo di = new DirectoryInfo(spath);
while (di.Exists)
{
	Thread.Sleep(100);
	di.Refresh();
}
Console.WriteLine("Folder moved");

Open in new window

0
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34230712
of course this code should be be placed after you call Directory.Move
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 1000 total points
ID: 34232495
Other way of doing the same is using Asynchronous Delegate Callback using IAsyncCallback method.

public delegate void MyDelegate;

public class MyClass
{
private MyDelegate del;
public MyClass()
{
del=new MyDelegate(this.ValidateMove);
}

public void StartChecking()
{
del.BeginInvoke(null, new AsyncCallBack(MoveFinished),null);
}

private void MoveFinished()
{
//Move has been finished
}

private void ValidateMove()
{
DirectoryInfo di=new Directoryinfo("UrPath");
while(di.Exists)
{
di.Refresh();
}
}

}

the above code allows you to asynchonously start waiting for move finished, and will call Move finished method after it is done.

Try reading about Asynchronous Delegates Callback.
Will give you more detail of what i am trying to do here.

hope it helps
:-)
0
 

Author Closing Comment

by:andre72
ID: 34318914
Thanks
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question