473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ 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.Ful lPath,txtProces sedFile.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 4086
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.Full Path, txtProcessedFil e.Text + e.Name);
}

private bool SaveFile (string source, string destination)
{
try
{
File.Move(e.Ful lPath,txtProces sedFile.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.b g> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.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.Ful lPath,txtProces sedFile.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(fil ename) in a manner similar to this:

char c = 'a';

string filename = txtProcessedFil e.Text + e.Name;

while(File.Exis ts(filename)
{
filename = txtProcessedFil e.Text + e.Name + c;
c++;
}

File.Move(e.Ful lPath, txtProcessedFil e.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
7743
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 ---------------------------------------------------------------------------- ----------------- import re, os, time, shutil os.chdir(os.environ+os.environ+"\\Desktop") DESKTOP =...
2
1810
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: "D:\dhc_work\classified\" FileName: "pinkf065111-26092005632633405685338094~LWS1A_20050209_105314.pdf.txt" InputFile: InputFolder + FileName OutputFile: OutputFolder...
1
3041
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 code where I do the file.move is taking a very long time (in the order of around 7 seconds for a simple 30kb file). The only thing that I can...
3
3714
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 streamwriter _fileWriter.Close()
1
2568
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? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been...
6
1907
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
7135
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 the "System.IO.Move" goes in to a loop and keeps creating the file on the destination folder with a 0byte size and keeps entering the error in the...
3
3269
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 (i.e. the root directory). This is the code: <script runat="server"> Sub MoveFile(ByVal obj As Object, ByVal ea As EventArgs)...
1
4746
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 Visual Basic Script file, call it "Move and Reformat Text File.VBS," that will run from a Windows Script Host command-prompt-based version as follows: ...
4
3260
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 creating each file, it moves the file to folder B, using shutil.move(). Thread T2, takes files from folder B and processes it. Note: Only the...
0
7496
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7428
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
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 we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.