c# - Copy executing files from a UNC path -
i writing application needs copy files network directory, files executing.
i have tried opening file with
using (var source = new filestream(filedata.filename, filemode.open, fileaccess.read, fileshare.readwrite))
after open file stream create stream copy into.
however, throws exception saying file in use process. not sure how around problem. if use file explorer copy files fine. know possible, not sure on how.
edit: have tried simple file.copy(source, destination)
, same exception saying file in use process.
to copy file being used process, have make use of 2 services in windows, , you'll need verify these services not disabled:
- volume shadow copy
- microsoft software shadow copy provider
they can left manual startup, don't need running time.
for this, can use 3rd party tool. hobocopy 1 of them. start 2 services automatically when needed, , volume shadow copy service turned off after it's done.
download link: https://github.com/candera/hobocopy/downloads
unzip file , place them lib directory of project. call process using system.diagnostics.process. in case, use following switch argument: /y (don't prompt, copy everything)
command-line syntax:
hobocopy [/statefile=file] [/verbosity=level] [ /full | /incremental ] [ /clear ] [ /skipdenied ] [ /y ] [ /simulate ] [/recursive] src dest [file [file [ ... ] ]
c# code:
process process = new process(); process.startinfo.useshellexecute = false; process.startinfo.createnowindow = true; process.startinfo.filename = @"lib\hobocopy.exe"; process.startinfo.arguments = @" /y <network-directory> <local-directory> <filename>"; process.start();
this'll copy files (hobocopy focuses on copying directories efficiently) being used or unused destination.
Comments
Post a Comment