473,480 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File.Move

edi
Hi,

I'm trying to move a file using File.Move(path1,path2). But the file
does not close immediately I call the method (I use Word library to
manage word file).
I can do something like this:

tryagain:
try
{
File.Move(e.FullPath,txtProcessedFile.Text+e.Name) ;
}
catch
{
goto tryagain;
}

Is there any alternative of this (I don't want to use "goto")?

Thanks.

Nov 17 '05 #1
2 4077
There are two problems with your code. The first one, you have identified:
Using goto is not generally an acceptable option. The second problem is
that you do not have any means of breaking your loop. If there is a problem
saving the file, the loop will continue forever.

To fix your code, I suggest that you put your save functionality in another
method and use a while loop to call the method something like this:

bool done = false;
while (!done)
{
done = SaveFile(e.FullPath, txtProcessedFile.Text + e.Name);
}

private bool SaveFile (string source, string destination)
{
try
{
File.Move(e.FullPath,txtProcessedFile.Text+e.Name) ;

// Insert code to verify that the new file actually exists here and
if it does:
return true;
}
catch
{
// Insert code here that can break the while loop - in other words,
return true if the user
// doesn't want to try saving again. Otherwise:
return false;
}
}

HTH

Dale Preston
MCAD, MCDBA, MCSE

"edi" <ed******@abv.bg> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi,

I'm trying to move a file using File.Move(path1,path2). But the file
does not close immediately I call the method (I use Word library to
manage word file).
I can do something like this:

tryagain:
try
{
File.Move(e.FullPath,txtProcessedFile.Text+e.Name) ;
}
catch
{
goto tryagain;
}

Is there any alternative of this (I don't want to use "goto")?

Thanks.

Nov 17 '05 #2
Hi edi,

As DalePres said, simply looping through the same exception will get you
nowhere.

Chances are the new filename is occupied, for file moving like this I
prefer to use File.Exists(filename) in a manner similar to this:

char c = 'a';

string filename = txtProcessedFile.Text + e.Name;

while(File.Exists(filename)
{
filename = txtProcessedFile.Text + e.Name + c;
c++;
}

File.Move(e.FullPath, txtProcessedFile.Text + e.Name);

This way the code will keep trying to move the file, but for every loop
the new filename is changed ever so slightly until an unoccupied filename
is found. Use a character or a number or any other means to create new
file names.

PS! I don't think this affects you, but when dealing with files, you
can't be 100% sure the file will be moved/deleted instantly as Windows
itself has a miniscule delay. Changing a filename to a file that was
just deleted might cause an exception.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7736
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
2
1802
by: Matthew Morvant | last post by:
I have am working with a text file and wish to move the file when I am done working with it. I have the following defined: InputFolder: "D:\dhc_work\indexed\" OutputFolder:...
1
3034
by: Matthew Eno | last post by:
I have a problem where I'm developing some code that moves files from one place to another (on the same drive) with the destination file having a new name. What's happening is that the line of...
3
3698
by: Don | last post by:
I have a strange bug popping up every once in a while. I've got a section of code that executes these statements when working with a streamwriter: --- Start --- .... ' Close the...
1
2553
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
6
1903
by: lilcech | last post by:
How can i cobine my image with a html file so i only have one file. The image is used inside the html file so if i delete it or move it, then it wont show up in the html file. :)
1
7128
by: kkizer | last post by:
I have a problem with this simple code below. Moving the files works perfectly, but once in a blue moon the file that it is trying to move dissapears before it can move it. and when this happens...
3
3263
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
1
4743
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
4
3251
by: Roopesh | last post by:
Hi, I have a multithreaded application. There are two threads, T1 and T2. Suppose that there are two folders A, B. Thread T1 fetches data from network and creates files in folder A and after...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.