Saturday, 15 August 2015

c# - Cannot move file because it used by another process -



c# - Cannot move file because it used by another process -

i move file after printed. using filesystemwatcher.

private void do_automatic() { filesystemwatcher watcher = new filesystemwatcher(); watcher.path = properties.settings.default.chemin; /* watch changes in lastaccess , lastwrite times, , renaming of files or directories. */ watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite | notifyfilters.filename | notifyfilters.directoryname; // watch text files. watcher.filter = properties.settings.default.type_fichier; // add together event handlers. //watcher.changed += new filesystemeventhandler(onchanged); watcher.created += new filesystemeventhandler(onchanged); // begin watching. watcher.enableraisingevents = true; // wait user quit program. //console.writeline("press \'q\' quit sample."); //while (console.read() != 'q') ; } // define event handlers. private void onchanged(object source, filesystemeventargs e) { //messagebox.show("file: " + e.fullpath + " " + e.changetype); dosavenprint(); } private void dosavenprint() { seek { string cheminboite = properties.settings.default.chemin; if (string.isnullorempty(cheminboite)) { messagebox.show("chémin boite est vide"); } else { directoryinfo di = new directoryinfo(cheminboite); fileinfo[] rgfiles = di.getfiles(); foreach (fileinfo fi in rgfiles) { switch (properties.settings.default.type_donne) { case "35-socah": outils.put_csvtosql_socah(cheminboite + "\\" + fi.name, id); break; case "50-lism": outils.put_csvtosql_lism(cheminboite + "\\" + fi.name, id); break; default: outils.put_csvtosql(cheminboite + "\\" + fi.name, id); break; } dictionary<string, int> odict = outils.getdataordre(); outils.print_boneticket(true, outils.get_rightprinter(properties.settings.default.zebra), odict["no_ordre"], odict["nbr_colis"]); string sourcefile = cheminboite + "\\" + fi.name; string destifile = @"c:\soft8_donne\histo\" + datetime.now.tostring("dd_mm_yyyy__hh_mm_ss___") + fi.name; waitforfile(sourcefile, destifile); } getdata(); outils.insrt_journax(sql_instjounaux, outils.get_computername().trim(), "getdatanprint", 3, "3", "reussi"); if (!properties.settings.default.is_auto) { messagebox.show("la sauvegarde été effectuée"); } } } grab (exception excthrown) { messagebox.show(excthrown.message + environment.newline + "veuillez prevenir french republic alliance "); outils.insrt_journax(sql_instjounaux, outils.get_computername().trim(), "problème getdatanprint", 3, "3", excthrown.message); } } /// <summary> /// blocks until file not locked more. /// </summary> /// <param name="fullpath"></param> bool waitforfile(string fullpathsource, string fullpathdesti) { int numtries = 0; while (true) { ++numtries; seek { file.move(fullpathsource, fullpathdesti); homecoming true; } grab (exception ex) { messagebox.show(string.format("waitforfile {0} failed exclusive lock: {1}", fullpathsource, ex.tostring())); if (numtries > 10) { messagebox.show(string.format("waitforfile {0} giving after 10 tries", fullpathsource)); homecoming false; } // wait lock released system.threading.thread.sleep(500); } } }

with current code error because file beingness used process.

c# printing filesystemwatcher file-in-use

No comments:

Post a Comment