System.IO.File.Copy Throws IOException -
i trying copy files:
private void docopy() { string[] files = directory.getfiles(application.startuppath + "\\app_data", "*.*", searchoption.alldirectories); string sftptoreadfilefrom = "ftp://<user>:<pass>@mysite.tk/updates/app_data/"; string spathtowritefileto = application.startuppath + "\\app_data"; webclient webclient = new webclient(); webclient.credentials = new networkcredential("user", "pass"); foreach (string s in files) { string filename = path.getfilename(s); string destfile = path.combine(spathtowritefileto, filename); byte[] filedata = webclient.downloaddata(sftptoreadfilefrom + filename); //shows correct bytes file.copy(s, destfile, true); } }
the exact error is: process cannot access file 'c:\applauncher\applauncher\bin\debug\app_data\firstfile' because being used process.
i followed 'msdn how to' here: http://msdn.microsoft.com/en-us/library/cc148994.aspx
if finds immediate red flags please let me know.
this how see it:
you want download file ftp server , write local disk. doing taking files in source-directory targets, won't work @ all. if files exist there, if can acquire file names there. (hence exception)
here must do
connect ftp, acquire files there (their bytes) , create files on disk.
private void docopy() { //string[] files = directory.getfiles(application.startuppath + "\\app_data", "*.*", searchoption.alldirectories); //acquire filenames ftp-server instead of local disk! string sftptoreadfilefrom = "ftp://<user>:<pass>@mysite.tk/updates/app_data/"; string spathtowritefileto = application.startuppath + "\\app_data"; webclient webclient = new webclient(); webclient.credentials = new networkcredential("user", "pass"); foreach (string s in files) { string filename = path.getfilename(s); //create file names based on ftp-server string destfile = path.combine(spathtowritefileto, filename); byte[] filedata = webclient.downloaddata(sftptoreadfilefrom + filename); //shows correct bytes //file.copy(s, destfile, true); rather use file.writeallbytes file.writeallbytes(destfile, filedata); } }
see here example of file.writeallbytes
.
the acquiring of filenames ftp not straightforward, though. there ftpwebrequest
-class support you.
Comments
Post a Comment