/// <summary> /// Sets a record stream field from a Stream object. Stream data cannot be inserted into temporary fields. /// </summary> /// <param name="field">Specifies the field of the Record to set.</param> /// <param name="stream">Specifies the stream data.</param> /// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the /// number of fields in the Record.</exception> /// <remarks><p> /// The stream persists if the Record is inserted into the Database and the Database is committed. /// </p><p> /// Win32 MSI API: /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstream.asp">MsiRecordsetStream</a> /// </p></remarks> public void SetStream(int field, Stream stream) { this.CheckRange(field); if (stream == null) { uint ret = RemotableNativeMethods.MsiRecordSetStream((int)this.Handle, (uint)field, null); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } else { Stream writeStream = null; string tempPath = Path.GetTempFileName(); try { writeStream = new FileStream(tempPath, FileMode.Truncate, FileAccess.Write); byte[] buf = new byte[512]; int count; while ((count = stream.Read(buf, 0, buf.Length)) > 0) { writeStream.Write(buf, 0, count); } writeStream.Close(); writeStream = null; uint ret = RemotableNativeMethods.MsiRecordSetStream((int)this.Handle, (uint)field, tempPath); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } } finally { if (writeStream != null) { writeStream.Close(); } if (File.Exists(tempPath)) { try { File.Delete(tempPath); } catch (IOException) { if (this.view != null) { this.view.Database.DeleteOnClose(tempPath); } } } } } }
/// <summary> /// Sets a record stream field from a file. Stream data cannot be inserted into temporary fields. /// </summary> /// <param name="field">Specifies the field of the Record to set.</param> /// <param name="filePath">Specifies the path to the file containing the stream.</param> /// <exception cref="ArgumentOutOfRangeException">The field is less than 0 or greater than the /// number of fields in the Record.</exception> /// <remarks><p> /// The contents of the specified file are read into a stream object. The stream persists if /// the Record is inserted into the Database and the Database is committed. /// </p><p> /// To reset the stream to its beginning you must pass in null for filePath. /// Do not pass an empty string, "", to reset the stream. /// </p><p> /// Setting a stream with this method is more efficient than setting a field to a /// FileStream object. /// </p><p> /// Win32 MSI API: /// <a href="http://msdn.microsoft.com/library/en-us/msi/setup/msirecordsetstream.asp">MsiRecordsetStream</a> /// </p></remarks> public void SetStream(int field, string filePath) { this.CheckRange(field); if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException("filePath"); } uint ret = RemotableNativeMethods.MsiRecordSetStream((int)this.Handle, (uint)field, filePath); if (ret != 0) { throw InstallerException.ExceptionFromReturnCode(ret); } }