Solved

How to check if a directory move had been finished

Posted on 2010-11-29
4
547 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

18 Experts available now in Live!

Get 1:1 Help Now