Solved

How to check if a directory move had been finished

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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