/// <summary> /// Compiles a new Package File from the currently stored Information /// </summary> /// <param name="ms">The Memory Stream you want to write</param> /// <param name="flname">Filename for the Package</param> protected void Save(MemoryStream ms, string flname) { StreamFactory.CloseStream(flname); string tmpfile = System.IO.Path.GetTempFileName(); try { // Try to save to a temp file System.IO.FileStream fs = new FileStream(tmpfile, FileMode.Create); try { Save(ms, fs); } finally { fs.Close(); fs.Dispose(); fs = null; } // If the destination already exists... if (System.IO.File.Exists(flname)) { // ...back up the current package content... if (Helper.WindowsRegistry.AutoBackup) { string bakfile = GetBakFileName(flname); if (System.IO.File.Exists(bakfile)) { System.IO.File.Delete(bakfile); } System.IO.File.Copy(flname, bakfile, true); } // ...and get rid System.IO.File.Delete(flname); } } catch (Exception ex) { System.IO.File.Delete(tmpfile); throw ex; } // At this point we have successfully written tmpfile and deleted flname // Rename the temp file to the destination System.IO.File.Move(tmpfile, flname); StreamFactory.UseStream(flname, FileAccess.Read); }
/// <summary> /// Checks if the passed File is writable by the System /// </summary> /// <param name="flname">The FileName</param> /// <returns>true, if the File is writable</returns> public static bool CanWriteToFile(string flname, bool close) { if (!System.IO.File.Exists(flname)) { return(true); } StreamItem si = StreamFactory.UseStream(flname, System.IO.FileAccess.ReadWrite); bool res = (si.StreamState == StreamState.Opened); if (close && res) { si.Close(); } return(res); }