コード例 #1
0
        // <summary>
        /// 创建符合要求的文件
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private static IPortableDeviceValues GetRequiredPropertiesForContentType(string sourcePath, string parentId)
        {
            IPortableDeviceValues values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;


            _tagpropertykey WPD_OBJECT_PARENT_ID = new _tagpropertykey();

            WPD_OBJECT_PARENT_ID.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_PARENT_ID.pid   = 3;
            values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentId);


            _tagpropertykey WPD_OBJECT_SIZE = new _tagpropertykey();

            WPD_OBJECT_SIZE.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_SIZE.pid   = 11;


            FileInfo fileInfo = new FileInfo(sourcePath);

            values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong)fileInfo.Length);


            _tagpropertykey WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();

            WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ORIGINAL_FILE_NAME.pid   = 12;
            values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(sourcePath));


            _tagpropertykey WPD_OBJECT_NAME = new _tagpropertykey();

            WPD_OBJECT_NAME.fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_NAME.pid   = 4;
            values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(sourcePath));


            return(values);
        }
コード例 #2
0
        public void Upload(IWpdObject containerObject, string sourceFilePath, bool overwrite)
        {
            var fileInfo = new FileInfo(sourceFilePath);

            if ((fileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                this.Upload(containerObject, sourceFilePath, overwrite, "*", true, false);
                return;
            }

            var fileName = Path.GetFileName(sourceFilePath);
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceFilePath);
            var targetPath = containerObject.GetPath() + Path.DirectorySeparatorChar + fileName;

            var children = containerObject.GetChildren();

            foreach (var child in children)
            {
                if ((String.Compare(child.OriginalFileName, fileName, true) == 0))
                {
                    if (overwrite)
                    {
                        this.Delete(child);
                        break;
                    }
                    else
                    {
                        var ex = new IOException(String.Format("A file with the path \"{0}\" already exists on the device.", targetPath));

                        if (this.DataCopyError != null)
                        {
                            this.DataCopyError(this, new DataCopyErrorArgs(sourceFilePath, targetPath, ex));
                        }

                        throw ex;
                    }
                }
            }

            if (this.DataCopyStarted != null)
            {
                this.DataCopyStarted(this, new DataCopyStartedArgs(sourceFilePath, targetPath));
            }

            var values = new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

            // Parent ID of the new object.
            values.SetStringValue(ref PortableDevicePKeys.WPD_OBJECT_PARENT_ID, containerObject.ObjectID);

            // Size in bytes of the new object.
            values.SetUnsignedLargeIntegerValue(PortableDevicePKeys.WPD_OBJECT_SIZE, (ulong)fileInfo.Length);

            // The original file name of the object.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_ORIGINAL_FILE_NAME, fileName);

            // The name of the object on the device.
            values.SetStringValue(PortableDevicePKeys.WPD_OBJECT_NAME, fileNameWithoutExtension);

            IStream targetStream             = null;
            var     optimalTransferSizeBytes = 0U;

            containerObject.Content.CreateObjectWithPropertiesAndData(values, out targetStream, ref optimalTransferSizeBytes, null);

            try
            {
                using (var sourceStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
                {
                    var buffer       = new byte[optimalTransferSizeBytes];
                    var bytesRead    = 0;
                    var totalWritten = 0UL;

                    do
                    {
                        bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);
                        if (bytesRead > 0)
                        {
                            var written = 0U;
                            targetStream.RemoteWrite(ref buffer[0], (uint)bytesRead, out written);
                            totalWritten += (ulong)written;

                            if (this.DataCopied != null)
                            {
                                this.DataCopied(this, new DataCopiedArgs(sourceFilePath, targetPath, (ulong)fileInfo.Length, totalWritten, (ulong)written));
                            }
                        }
                    }while (bytesRead > 0);
                }
                targetStream.Commit(0);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(String.Format("Failed to upload file \"{0}\": {1}", sourceFilePath, ex));

                if (this.DataCopyError != null)
                {
                    this.DataCopyError(this, new DataCopyErrorArgs(sourceFilePath, targetPath, ex));
                }
            }
            finally
            {
                if (targetStream != null)
                {
                    Marshal.ReleaseComObject(targetStream);
                }
            }

            if (this.DataCopyEnded != null)
            {
                this.DataCopyEnded(this, new DataCopyEndedArgs(sourceFilePath, targetPath));
            }
        }
コード例 #3
0
        protected IPortableDeviceValues GetRequiredPropertiesForContentType(string fileName,
                                                string parentObjectId)
        {
            IPortableDeviceValues values =
                new PortableDeviceTypesLib.PortableDeviceValues()
                as IPortableDeviceValues;

            var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
            WPD_OBJECT_PARENT_ID.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                            0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_PARENT_ID.pid = 3;
            values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

            FileInfo fileInfo = new FileInfo(fileName);
            var WPD_OBJECT_SIZE = new _tagpropertykey();
            WPD_OBJECT_SIZE.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                            0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_SIZE.pid = 11;
            values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong)fileInfo.Length);

            var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
            WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                            0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
            values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(fileName));

            var WPD_OBJECT_NAME = new _tagpropertykey();
            WPD_OBJECT_NAME.fmtid =
                new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC,
                            0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
            WPD_OBJECT_NAME.pid = 4;
            values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(fileName));

            //From Original Tutorial:
            //  "Remark: To keep it easy I extracted the filename and
            //   path of the book and used that to set both the
            //   WPD_OBJECT_ORIGINAL_FILE_NAME and
            //   WPD_OBJECT_NAME properties.

            return values;
        }