Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Cutting Files and Pasting in New Directory

Tags:

c#

I am trying to cut files from one directory on my computer and paste it into a new directory every time a file is created in this specific directory. I am already watching the directory would it be something simple like

if (e.ChangeType == System.IO.WatcherChangeTypes.Created)
{ 
    //cut file
    //paste into new directory            
}
like image 461
Bgear Avatar asked Oct 13 '10 18:10

Bgear


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

indeed, cut + paste is equivalent to move, so System.IO.File.MoveTo() should handle your problem quite well

like image 133
vlad Avatar answered Sep 21 '22 21:09

vlad


First a clarification, "cut and paste" are user interface terms. In the file system, it's referred to as "moving" the file.

Second, the suggestions to use File.MoveTo may not work as you would like. The IO Watcher will tell you when a file is first created, not when it is done being written to. If you immediately move a file, you may yank it out from underneath the writer. This could result in an error in the program which is writing to the file.

The correct way to do what you are proposing is to wait until the file has been closed by the process that is writing to it, and then move it.

Have a look at the LockFileEx method in MSDN. You can use it to block until you have an exclusive lock on the file (which will happen when the writing process closes the file). Then move the file before releasing the lock.

Finally, you probably don't want to call LockFileEx from within the file system watcher callback. You may have to wait a very long time before you get the exclusive lock on the file. Instead you should queue the created files to be locked and moved by a secondary thread (or the thread pool).

like image 25
Kennet Belenky Avatar answered Sep 23 '22 21:09

Kennet Belenky