c# - Could not find a part of the path 'C:\destPath\FileName.ext' when copying directories -
so, have logic looks following:
private static void copytodestdirectory(string destdirectory, string srcdirectory) { if (!directory.exists(destdirectory)) { directory.createdirectory(destdirectory); } foreach (var file in directory.getfiles(srcdirectory)) { var filename = path.getfilename(file); var destpath = path.combine(destdirectory, filename); file.copy(file, destpath, true); } ... }
usually works, every once in while not. times not work, have log file resembles following:
2016-12-22 15:49:21,670 [11] error: system.io.directorynotfoundexception: not find part of path 'c:\destpath\filename.ext'. @ system.io.__error.winioerror(int32 errorcode, string maybefullpath) @ system.io.file.internalcopy(string sourcefilename, string destfilename, boolean overwrite, boolean checkhost) @ system.io.file.copy(string sourcefilename, string destfilename, boolean overwrite) @ namespace.class.copytodestdirectory(string destdirectory, string srcdirectory) @ namespace.class.callingmethod()
i have no idea why i'm getting exception destination path. doesn't make sense me because make sure recreate destination directory before copying.
this calling method looks like. it's notable in deletes dest directory before calling copytodestdirectory
wouldn't expect problem because, , recreate directory again, , because both , copytodestdirectory
synchronous, not familiar of low-level details behind system.io functions, think it's worth mentioning.
public void callingmethod() { try { ... if (directory.exists(destdirectory)) { directory.delete(destdirectory, true); } string srcdirectory = getsrcdirectory(); copytodestdirectory(destdirectory, srcdirectory); ... } catch (exception ex) { _log.error("", ex); throw; } }
what might cause error this?
i had similar case powershell before create symlinks/junctions renewed content of directory.
as workaround: i´ve ended in writing loop, delete/create directory while exists/or not. wanted write log, loop case, since changes worked @ first time. strange, maybe mistake in code around, hadn´t reproduce it. maybe it´s enough call directory.exists() second time.
Comments
Post a Comment