• 0

File Access Question (C#)


Question

Hello everyone.

I have an application that zips up some files and then attempts to send that in an e-mail as an attachment.

I have noticed that if the zip file is small it works fine (A few KB's), but the zip is generally between 1 and 2 MB's, and at that size (Which is still rather small) it fails. I get an error that it failed due to the target of an invocation.

I'm assuming that perhaps the zip file is not being released prior to the e-mail being generated when it's larger? I have both the zip creation and e-mail generation in their own backGroundWorkers.

I've been working on this for a good while now, and it doesn't seem to matter if my AntiVirus is running (I thought maybe it was trying to scan the zip), if I do a thread.Sleep, if I choose to send the mail upon the click of another button, etc. In fact, curiously, I created a test application that ONLY sends the zip file. This works perfectly. However, if I use my main application to create the zip file, this test application will be unable to send the zip until after the main application is closed. I really think that the file is not being released, but I don't know why, or from what. The backgroundWorker that is creating the zip is completing successfully (That is where I was originally calling the SendMail event).

My question is what the suggested course of action would be for doing something like this. I've never had an issue with accessing a file like this before.

I really appreciate any suggestions anyone may have for this. I can post whatever code you want, but there's a lot of it, so I'm not sure what all you would want.

Thanks Again,

Link to comment
https://www.neowin.net/forum/topic/846100-file-access-question-c/
Share on other sites

11 answers to this question

Recommended Posts

  • 0

My first train of thought would be to remove it from the backgroundworker and just run the whole thing in a single thread, see if that makes a difference. I would say its worth having a look at your code too and making sure that you're releasing any file handles that you're creating in the process.

Might be worth posting some relevant code (if you can) so we can take a look.

  • 0
what is you're code for creating the zip file?

i like to use the ShapZipLib .NET Zip Library personally, have you given that a go?

can't really do much without knowing what your code is up to so, i await the response :)

Thank you very much for your response. I haven't tried that. I've been using DotNetZip Library (http://dotnetzip.codeplex.com/). It's been really good, and after mentioning this to the developer, it doesn't appear to be related to any of DotNetZip's code...

Here is the code for creating the zip:

		void CompileZip()
		{
			try
			string tempFolder = Path.GetTempPath();
			if (File.Exists(tempFolder + @"\JobFile.zip"))
			{
					SendMail()
			 }
			 else
			 {
					string[] tempDirectory = { tempFolder };
					CompileZipbackgroundWorker2.RunWorkerAsync(tempDirectory);
					Application.DoEvents();
			 }
		}

		private void CompileZipbackgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
		{
			BackgroundWorker worker = sender as BackgroundWorker;
			string[] tempDirectory = (string[])e.Argument;
			string FilePath = tempDirectory[0].ToString();

			string current = Directory.GetCurrentDirectory();
			System.IO.Directory.SetCurrentDirectory(FilePath);

			//int progress = 10;

			using (ZipFile zip = new ZipFile())
			{
				//Add Generated Files to zip
				zip.AddItem("JobFile");
				zip.AddItem("Resources");

				worker.ReportProgress(20);
				worker.ReportProgress(90);

				//Save the zip
				zip.Save(Path.Combine(FilePath, "JobFile.zip"));
			}
			Application.DoEvents();
			worker.ReportProgress(100);
			System.IO.Directory.SetCurrentDirectory(current);
		}

		private void CompileZipbackgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			SubProgressBar1.Value = e.ProgressPercentage;
		}

		private void CompileZipbackgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			if ((e.Cancelled == true))
			{
				MessageBox.Show("Canceled", "Error", MessageBoxButtons.OK);
			}

			else if (!(e.Error == null))
			{
				MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK);
			}

			else
			{
				string tempFolder = Path.GetTempPath();
				string zipedFile = tempFolder + @"\JobFile.zip";
				if (File.Exists(zipedFile))
				{
					try
					{
						SubProgressBar1.Style = ProgressBarStyle.Continuous;

						SendMail();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Error.Message, "Error", MessageBoxButtons.OK);

						return;
					}
				}
			}
		}

Here's the code for sending the E-Mail:

		void SendMail()
		{
			try
			{
				SubProgressBar1.Style = ProgressBarStyle.Marquee;

				string ccState = "";
				if (CCCheckBox1.Checked == true)
				{
					ccState = "true";
				}
				else
				{
					ccState = "false";
				}

				string urgentState = null;
				if (UrgentCheckBox1.Checked == true)
				{
					urgentState = "true";
				}
				else
				{
					urgentState = "false";
				}

				string attachState = null;
				if (SubmitStepTB1.Text == "Attach")
				{
					attachState = "true";
				}
				else
				{
					attachState = "false";
				}

				string[] SendMail = { CustLastNameTB1.Text, CustFirstNameTB1.Text, UserCB1.Text, UserEMailTB1.Text, AssemblyVersion, JobFileNotesTB1.Text, ccState, urgentState, attachState };
				SendMailbackgroundWorker1.RunWorkerAsync(SendMail);
			}
			catch
			{

			}
		}

		private void SendMailbackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
		{
			try
			{
				string[] SendMail = (string[])e.Argument;
				string CustLastName = SendMail[0].ToString();
				string CustFirstName = SendMail[1].ToString();
				string User = SendMail[2].ToString();
				string UserEMail = SendMail[3].ToString();
				string SoftVersion = SendMail[4].ToString();
				string JobFileNotes = SendMail[5].ToString();
				string CCState = SendMail[6].ToString();
				string UrgentState = SendMail[7].ToString();
				string attachState = SendMail[8].ToString();

				//Send E-Mail on click of Button
				MailMessage theMailMessage = new MailMessage("FromEMail", "ToEMail");

				BackgroundWorker worker = sender as BackgroundWorker;
				//worker.ReportProgress(25);

				//Generate the message body
				string messageBody = "New Job File Information Added For:  " + CustLastName + ", " + CustFirstName;
				messageBody += Environment.NewLine + "";
				messageBody += Environment.NewLine + "From:  " + User;
				messageBody += Environment.NewLine + "E-Mail:  " + UserEMail + "  (CC'd =  " + CCState + ")";
				messageBody += Environment.NewLine + "";
				messageBody += Environment.NewLine + "Software Version:  " + SoftVersion;
				messageBody += Environment.NewLine + "";
				messageBody += Environment.NewLine + @"\\server\shared\job files\" + CustLastName + ", " + CustFirstName + @"\";
				messageBody += Environment.NewLine + "";
				messageBody += Environment.NewLine + "Notes:  " + JobFileNotes;

				//worker.ReportProgress(50);

				//Set the property of the message body and subject body
				theMailMessage.Body = messageBody;
				theMailMessage.Subject = "New Job File for " + CustLastName + ", " + CustFirstName;

				//Set the CC Property
				if (CCState == "true")
				{
					MailAddress copy = new MailAddress(UserEMail);
					theMailMessage.CC.Add(copy);
				}

				//Set the Urgent Property
				if (UrgentState == "true")
				{
					theMailMessage.Priority = MailPriority.High;
				}

				//Set the Attachment Property
				if (attachState == "true")
				{
					string tempFolder = Path.GetTempPath();
					theMailMessage.Attachments.Add(new Attachment(tempFolder + @"\JobFile.zip"));
				}

				//worker.ReportProgress(75);

				//E-Mail Credentials and Sending
				SmtpClient theClient = new SmtpClient("smtp.1and1.com");
				System.Net.NetworkCredential theCredential = new
				System.Net.NetworkCredential("FromEMail", "Password");
				theClient.Credentials = theCredential;
				theClient.Send(theMailMessage);

				//worker.ReportProgress(100);
			}
			catch
			{
				MessageBox.Show("The Mail Message was unable to be sent.", "Error", MessageBoxButtons.OK);
			}
		}

		private void SendMailbackgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
			SubProgressBar1.Value = e.ProgressPercentage;
		}

		private void SendMailbackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			SubProgressBar1.Increment(100);

			Application.DoEvents();
		}

That should be everything. I'm curious what your thoughts might be. The SendMail code does work (As evidenced by the test project I created), but not if the file has just been created.

Thanks Again. If there is anything else you might want to look at, please let me know. I really, REALLY appreciate the help. I'm just completely out of ideas here and it's driving me nuts...

Edited by M_Lyons10
  • 0

Thank you very much for the suggestion. I will try that to see if it is able to send the mail, but the reason I had initially used a backgroundWorker was because the UI was locking up. Additionally, as the zip process takes some time, the code was attempting to send the mail prior to the zip being completed.

Thanks Again,

  • 0
yeah i understand why the background process is there, but i think for the purposes of working out whats wrong it should be chopped.

it might also be related to having one end background process call another background process.. but i'm unsure

Thanks for your help and everything. I did rewrite the code yesterday / last night and was testing it. It locks up the UI something fierce, but it does work. So, the backgroundWorker is not terminating? Or is somehow holding onto the file? I thought that when a backgroundWorker completed it was done / terminated. I'm not sure what the difference would possibly be. I had run the application a bit ago while watching the file access of the zip file using Process Monitor and it was closing...

You say it's the "sendmail" that's failing. Many ISPs restrict the size of e-mails, so it's possible it's being rejected by the server because the payload is too large.

I realize that some do restrict attachment size, but I've never seen such a small limit (1 - 2 MB's). Just to test that though, I did make a test application that sends the zip file that has already been compiled. It works perfectly so long as the application that created the zip has exited.

Thanks Again,

  • 0

first thing first ... if you get an exception and you are working with backgroundworkers, check the InnerException property - it should encapsulate what exception was raised in the background worker.

Secondly, since you have your send mail worker code wrapped up nicely in a try...catch clause, your assumption that the file is not release properly is void (+ the fact that you are using "using" clause when building the zip file helps in that it will release any stuff used by ZipFile immediatly (if coded properly by the DotNetZip dev))

I "tried" to replicate as close as possible the requirements of the code you posted (basically a progress bar, a button and a few checkboxes) to try to replicate the issue, and from what I can tell, you are trying to Increment the progress bar value when it is in Marquee Mode.

Hinty: Its a good idea to use BackgroundWorker for these kind of situations - situations that involve a UI and length operations so stick to it :) Just when you are handling exceptions, try not to use catch-all exception handlers so that if there is some serious "unhandled" stuff going on in your worker, an exception will be raised, and that can help you out to find the cause and fix it.

  • 0
first thing first ... if you get an exception and you are working with backgroundworkers, check the InnerException property - it should encapsulate what exception was raised in the background worker.

Secondly, since you have your send mail worker code wrapped up nicely in a try...catch clause, your assumption that the file is not release properly is void (+ the fact that you are using "using" clause when building the zip file helps in that it will release any stuff used by ZipFile immediatly (if coded properly by the DotNetZip dev))

I "tried" to replicate as close as possible the requirements of the code you posted (basically a progress bar, a button and a few checkboxes) to try to replicate the issue, and from what I can tell, you are trying to Increment the progress bar value when it is in Marquee Mode.

Hinty: Its a good idea to use BackgroundWorker for these kind of situations - situations that involve a UI and length operations so stick to it :) Just when you are handling exceptions, try not to use catch-all exception handlers so that if there is some serious "unhandled" stuff going on in your worker, an exception will be raised, and that can help you out to find the cause and fix it.

Thank you very much for your response. I will take a look at the InnerException property. I will look into the progressBar increment as well. That may have been an error on my part. The error being thrown the Visual Studio is that the mail message timed out. I will try to improve the catches to help a bit further. It's an odd error and I am not sure what the solution would really be. Programs perform processes like this all the time, but it's really giving me a headache.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • DiskGenius 6.2.0.1829 - All Versions: Free, Lite & Portable by Razvan Serea DiskGenius is a full-featured partition manager, which is designed to optimize disk usage for Windows users. It will efficiently help you recover lost data, resize/split partition, backup files, edit hex data, check bad sectors, manage virtual disks, erase data, etc.. Create a system image backup for current Windows with simple clicks to keep the operating system under protection. DiskGenius key features: Partition Management - It can create format, resize, extend, backup, split, hide and clone partition, both MBR and GPT are supported. Disk and partition conversion - Convert dynamic disk to basic, convert virtual disk format and convert MBR to GPT, convert primary partition to logical. File recovery - It can recover files deleted or emptied form recycle bin, recover files from damaged partition or disk and recover files by file type and supports file preview and file filter. Partition recovery - It is the best partition recovery program in that it can recover files from damaged, corrupted and RAW partitions, search for lost partition and recover files from it, besides, it can fix partition table. RAID recovery - It can reconstruct Virtual RAID and recover files from it, and all RAID types are supported. Sector Editor - A Hex editor is embedded to help users edit raw hex data and recover data manually. Backup and Restore - It can backup and restore partition including system partition, hard disk and partition table. Bad Tracks - It can check and repair bad sectors for all storage devices; check hard disk S.M.A.R.T. information. Delete files permanently - It can delete files permanently so that they can't be recovered by any data recovery software. Virtual Disk - It supports virtual disks, including VMware, Virtual PC and Virtual Box. Create WinPE bootable disk and you can manage disk partition when system crashes or there is no operating system on your computer. Support FAT12/FAT16/FAt32/exFAT/NTFS/EXT2/EXT3/EXT4 file system format. DiskGenius 6.2.0.1829 changelog: Add the "Disk Speed Test" feature. Add the "Windows Boot Repair and Conversion" feature. Add the BMB21-2019 erase standard to the "Erase Sectors" feature. Add support for restoring an individual partition from a PMFX disk image file. Enhanced The "Verify Or Repair Bad Sectors/Blocks" feature displays disk read speed in the detection window during scanning. The "Quick Partition" dialog box allows users to quickly select the number of partitions by pressing the numeric keys 1, 2, 7, 8, or 9. The "Set Volume Name" dialog box supports selecting preset volume labels provided by the software. The "Copy Sectors" feature supports resuming copy tasks after modifying the number of skipped bad sectors. Add the "TRIM Optimization" option to the format dialog box. The "Clone Partition" and "Clone Disk" features perform TRIM optimization on target partitions or disks before cloning. Add support for Not Equal To search conditions (prefixed with "!") when searching hexadecimal data in the sector editor. Optimize the display of capacity values in the program interface to show two decimal places. Add a minimize button to dialogs that may require long processing time. Enhance support for the ReFS file system. Enhance support for newer HIF and MP4 formats when recovering files by type. Enhance support for the EXT4 file system. Enhance compatibility of the "File Recovery" feature with special data structures. Fixed Fixed the issue that the selected file system type automatically reverted to NTFS after changing it to exFAT or EXT4 in the "Quick Partition" dialog box. Fixed inaccurate Unicode string search results in the "Sector Editor" feature. Fixed the issue that exceptions might occur when adding multiple disks in the "Erase Sectors" feature. Fixed the issue that insufficient target disk space was incorrectly reported in some cases when cloning, backing up, or restoring disks. Fixed the issue that folder modification timestamps were not preserved when copying files from ReFS partitions. Fixed the issue that Excel-format reports generated by features such as file copying or bad sector checking could not be opened when the report contained more than one million rows. Fixed the issue that folders were not displayed in the exclude-folder dialog box when backing up partitions to image files. Fixed the issue that the "Erase Sectors" feature could not be executed in some cases. Download: DiskGenius 6.2.0.1829 | 63.9 MB (Freeware, paid upgrade available) Download: DiskGenius Portable 64-bit | 40.0 MB Download: DiskGenius Portable 32-bit | 36.0 MB Download: DiskGenius Lite 64-bit | 13.4 MB Download: DiskGenius Lite 32-bit | 11.6 MB View: DiskGenius Home Page | DiskGenius Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Really? Use a better search engine https://www.google.com/search?...ourceid=chrome&ie=UTF-8
    • Seems like Neowin has transitioned into being simps for the white house. I can't find a review for the last UFC games that came out.
  • Recent Achievements

    • Week One Done
      agatameier earned a badge
      Week One Done
    • One Month Later
      agatameier earned a badge
      One Month Later
    • Week One Done
      ssd21345 earned a badge
      Week One Done
    • Contributor
      MarkHughes4096 went up a rank
      Contributor
    • Dedicated
      jordanspringer earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      513
    2. 2
      +Edouard
      182
    3. 3
      PsYcHoKiLLa
      143
    4. 4
      ATLien_0
      95
    5. 5
      Steven P.
      74
  • Tell a friend

    Love Neowin? Tell a friend!