private void reportError(Exception ex, bool displayMessage, bool logMessage) { //Report an exception to the user try { string src = (ex.Source != null) ? ex.Source + "-\n" : ""; string msg = src + ex.Message; if (ex.InnerException != null) { if ((ex.InnerException.Source != null)) { src = ex.InnerException.Source + "-\n"; } msg = src + ex.Message + "\n\n NOTE: " + ex.InnerException.Message; } if (displayMessage) { MessageBox.Show(msg, "PCS File Utility", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (logMessage) { SvcLog.LogMessage("ERROR\t" + ex.ToString()); } } catch (Exception) { } }
private void manageSourceFiles(FileInfo srcFolder) { //Move/copy all files in subfolder <srcFolder> that meet the file pattern try { //Enumerate source folder files string[] srcFiles = Directory.GetFiles(srcFolder.FullName, this.mSrcFilePattern); for (int i = 0; i < srcFiles.Length; i++) { //Move source file; rename for uniqueness FileInfo file = new FileInfo(srcFiles[i]); string destFilename = ""; try { if (file.Name.Substring(0, 1) != "_" && file.Length > 0) { //Move file and rename source destFilename = getDestinationFilename(); file.CopyTo(destFilename, false); SvcLog.LogMessage("MOVED FILE\t" + srcFiles[i] + "\t" + destFilename); file.MoveTo(file.Directory + "\\_" + file.Name + "_" + DateTime.Now.ToString("HHmmss")); } } catch (Exception ex) { SvcLog.LogMessage("MOVE FILE ERROR\t" + srcFiles[i] + "\t" + destFilename + "\t" + ex.Message); } } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tPCSSvc::manageSourceFiles(); " + ex.Message); } }
private void OnFormLoad(object sender, System.EventArgs e) { //Event handler for form load try { //Hide this.Visible = false; this.mService.Start(); } catch (Exception ex) { SvcLog.LogMessage("STARTUP ERROR\t" + ex.Message); } }
private void OnFormClosed(object sender, System.EventArgs e) { //Event handler for form closed try { //Close task tray icon if applicable; log application as stopped if (this.mIcon != null) { this.mIcon.Visible = false; } this.Visible = false; SvcLog.LogMessage("UTILTIY STOPPED"); } catch (Exception) { } finally { Application.Exit(); } }
public void Execute() { //Execute this service try { //Check for the specified source folder SvcLog.LogMessage("CHECKING FOR FILES IN " + this.mSrcFolder + "..."); if (Directory.Exists(this.mSrcFolder)) { //Perform file operations in source folder FileInfo oSrcFolder = new FileInfo(this.mSrcFolder); manageSourceFiles(oSrcFolder); } else { SvcLog.LogMessage("DIRECTORY NOT FOUND\t" + this.mSrcFolder); } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tFileSvc::Execute(); " + ex.Message); } }
private string getDestinationFilename() { //Determine a unique destination filename string filename = ""; try { //Ensure time-based filename is unique (i.e. next second) int day = DateTime.UtcNow.DayOfYear; int seconds = 3600 * DateTime.UtcNow.Hour + 60 * DateTime.UtcNow.Minute + DateTime.UtcNow.Second; filename = this.mDestFolder + day.ToString("000") + seconds.ToString("00000") + (this.mDestSuffix.Length > 0 ? this.mDestSuffix : ""); while (File.Exists(filename)) { //Wait until the next second in time System.Threading.Thread.Sleep(1000 - int.Parse(DateTime.Now.ToString("fff"))); //Determine next filename day = DateTime.UtcNow.DayOfYear; seconds = 3600 * DateTime.UtcNow.Hour + 60 * DateTime.UtcNow.Minute + DateTime.UtcNow.Second; filename = this.mDestFolder + day.ToString("000") + seconds.ToString("00000") + (this.mDestSuffix.Length > 0 ? this.mDestSuffix : ""); } } catch (Exception ex) { SvcLog.LogMessage("UNEXPECTED ERROR\tPCSSvc::getDestinationFilename(); " + ex.Message); } return(filename); }