?
Solved

How to check if a directory move had been finished

Posted on 2010-11-29
4
Medium Priority
?
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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