public static string saveToFile(string fileNameNoDate, MemoryStream _stream) { //string _path = Path.Combine(Path.GetTempPath(), // DateTime.Now.ToString("yy-MM-dd ") + // Helpers.getSafeFilename(fileNameNoDate) + // ".pdf"); //while (File.Exists(_path)) //ALWAYS GENERATE RANDOM FILENAME TO AVOID THREAD COLLISIONS string _path = Path.Combine( Path.GetTempPath(), DateTime.Now.ToString("yy-MM-dd ") + Helpers.getSafeFilename(fileNameNoDate) + ThreadSafeRandom.create().Next(99999).ToString() + ".pdf" ); byte[] bytes = _stream.ToArray(); if ((bytes.Length > 5 * 1024 * 1024) && MessageBox.Show("This PDF is larger than 5MB. Some iphones may not be able to return it via email. Do you wish to continue?", "", MessageBoxButton.YesNo) == MessageBoxResult.No) { return(null); } System.IO.File.WriteAllBytes(_path, bytes); return(_path); }