Solved

How to check if a directory move had been finished

Posted on 2010-11-29
4
551 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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now