Problem when moving a file

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

Hi,

I want to move a file. Here is my code:

files.MoveTo(dirs[0].ToString());

Variable files represents a file in the current directory (of the
executable).
Variable dirs[0] represents a subdirectory in the current directory.

When I run the code, my application 'hangs'. I don't know whats wrong.
The file and subdirectory exists.

Any help?

Thanks!
 
Arjen said:
Hi,

I want to move a file. Here is my code:

files.MoveTo(dirs[0].ToString());

Variable files represents a file in the current directory (of the
executable).
Variable dirs[0] represents a subdirectory in the current directory.

When I run the code, my application 'hangs'. I don't know whats wrong.
The file and subdirectory exists.

Any help?

Thanks!


To begin with it would've been nice if you explicitly stated the classes
you're using.

I'm assuming you are using the FileInfo.MoveTo method, and if you look
at the documentation for this, it expects a destination *file-name*, not
a directory.

As such, you need to specify what filename it should have in the target
directory as well.

Something like this:

files.MoveTo(Path.Combine(dirs[0].ToString(),
Path.GetFileName(files.Name)))

(note, untested, writing from documentation only)
 
Arjen said:
I want to move a file. Here is my code:
files.MoveTo(dirs[0].ToString());


Variable files represents a file in the current directory (of the
executable).
Variable dirs[0] represents a subdirectory in the current directory.

When I run the code, my application 'hangs'. I don't know whats wrong.
The file and subdirectory exists.
Any help?

To begin with it would've been nice if you explicitly stated the classes
you're using.

I'm assuming you are using the FileInfo.MoveTo method, and if you look
at the documentation for this, it expects a destination *file-name*, not
a directory.

As such, you need to specify what filename it should have in the target
directory as well.

Something like this:

files.MoveTo(Path.Combine(dirs[0].ToString(),
Path.GetFileName(files.Name)))

(note, untested, writing from documentation only)

--
Lasse Vågsæther Karlsen
mailto:[email protected]://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -


Thanks for your reply. This works! Thanks!
 
Back
Top