Пример #1
0
        /// <summary>
        /// Sets or removes icon.
        /// </summary>
        /// <param name="set">True to display the icon. False - to remove the icon.</param>
        private async Task SetIconAsync(bool set, int?id = null, string iconFile = null, string description = null)
        {
            IStorageItem storageItem = await FsPath.GetStorageItemAsync(userFileSystemPath);

            if (storageItem == null)
            {
                // This method may be called on temp files, typically created by MS Office, that exist for a short period of time.
                // StorageProviderItemProperties.SetAsync(null,) causes AccessViolationException
                // which is not handled by .NET (or handled by HandleProcessCorruptedStateExceptions) and causes a fatal crush.
                return;
            }

            try
            {
                if (set)
                {
                    StorageProviderItemProperty propState = new StorageProviderItemProperty()
                    {
                        Id           = id.Value,
                        Value        = description,
                        IconResource = Path.Combine(virtualDrive.Settings.IconsFolderPath, iconFile)
                    };
                    await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { propState });
                }
                else
                {
                    await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { });
                }
            }

            // Setting status icon failes for blocked files.
            catch (FileNotFoundException)
            {
            }
            catch (COMException)
            {
                // "Error HRESULT E_FAIL has been returned from a call to a COM component."
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2147024499)
                {
                    // "The operation failed due to a conflicting cloud file property lock. (0x8007018D)"
                }
                else
                {
                    // Rethrow the exception preserving stack trace of the original exception.
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Sets or removes icon.
        /// </summary>
        /// <param name="set">True to display the icon. False - to remove the icon.</param>
        private async Task SetIconAsync(bool set, int?id = null, string iconFile = null, string description = null)
        {
            IStorageItem storageItem = await FsPath.GetStorageItemAsync(userFileSystemPath);

            if (storageItem == null)
            {
                return; // Item does not exists.
            }

            try
            {
                if (set)
                {
                    StorageProviderItemProperty propState = new StorageProviderItemProperty()
                    {
                        Id           = id.Value,
                        Value        = description,
                        IconResource = Path.Combine(Program.Settings.IconsFolderPath, iconFile)
                    };
                    await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { propState });
                }
                else
                {
                    await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { });
                }
            }

            // Setting status icon failes for blocked files.
            catch (FileNotFoundException)
            {
            }
            catch (COMException)
            {
                // "Error HRESULT E_FAIL has been returned from a call to a COM component."
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2147024499)
                {
                    // "The operation failed due to a conflicting cloud file property lock. (0x8007018D)"
                }
                else
                {
                    // Rethrow the exception preserving stack trace of the original exception.
                    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw();
                }
            }
        }
Пример #3
0
        public static async void ApplyCustomStateToPlaceholderFile(string path, string filename, StorageProviderItemProperty prop)
        {
            try
            {
                var fullPath = Path.Combine(path, filename);

                var customProperties = new StorageProviderItemProperty[] { prop };

                var item = await StorageFile.GetFileFromPathAsync(fullPath);

                await StorageProviderItemProperties.SetAsync(item, customProperties);
            }
            catch (Exception ex)
            {
                // winrt.to_hresult() will eat the exception if it is a result of winrt.check_hresult, otherwise the exception will get
                // rethrown and this method will crash out as it should
                Console.Write("Failed to set custom state with {0:X8}\n", ex.HResult);
            }
        }
        /// <summary>
        /// Sets or removes "Download pending" icon.
        /// </summary>
        /// <param name="set">True to display the icon. False - to remove the icon.</param>
        private async Task SetDownloadPendingIconAsync(bool set)
        {
            IStorageItem storageItem = await FsPath.GetStorageItemAsync(userFileSystemPath);

            if (set)
            {
                // Set upload pending icon.

                // Set download pending icon.
                StorageProviderItemProperty propState = new StorageProviderItemProperty()
                {
                    Id           = 2,
                    Value        = "Download from server pending", // "@propsys.dll,-42291",
                    IconResource = "%systemroot%\\system32\\imageres.dll,-1402"
                };
                await StorageProviderItemProperties.SetAsync(storageItem, new[] { propState });
            }
            else
            {
                await StorageProviderItemProperties.SetAsync(storageItem, new StorageProviderItemProperty[] { });
            }
        }
Пример #5
0
        public static void Create(string sourcePathStr, string sourceSubDirStr, string destPath)
        {
            try
            {
                WIN32_FIND_DATA findData;

                // Ensure that the source path ends in a backslash.
                // Ensure that a nonempty subdirectory ends in a backslash.
                var fileName = Path.Combine(sourcePathStr, sourceSubDirStr, "*");

                using var hFileHandle = FindFirstFileEx(fileName, FINDEX_INFO_LEVELS.FindExInfoStandard, out findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, default, FIND_FIRST.FIND_FIRST_EX_ON_DISK_ENTRIES_ONLY);
                if (!hFileHandle.IsInvalid)
                {
                    do
                    {
                        if (findData.cFileName == "." || findData.cFileName == ".." || (findData.dwFileAttributes & FileAttributes.Hidden) != 0)
                        {
                            continue;
                        }

                        var relativeName = Path.Combine(sourceSubDirStr, findData.cFileName);
                        using var pRelativeName = new SafeCoTaskMemString(relativeName);
                        var cloudEntry = new CF_PLACEHOLDER_CREATE_INFO
                        {
                            FileIdentity       = pRelativeName,
                            FileIdentityLength = pRelativeName.Size,
                            RelativeFileName   = relativeName,
                            Flags      = CF_PLACEHOLDER_CREATE_FLAGS.CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC,
                            FsMetadata = new CF_FS_METADATA
                            {
                                FileSize  = Macros.MAKELONG64(findData.nFileSizeLow, unchecked ((int)findData.nFileSizeHigh)),
                                BasicInfo = new FILE_BASIC_INFO
                                {
                                    FileAttributes = (FileFlagsAndAttributes)findData.dwFileAttributes,
                                    CreationTime   = findData.ftCreationTime,
                                    LastWriteTime  = findData.ftLastWriteTime,
                                    LastAccessTime = findData.ftLastAccessTime,
                                    ChangeTime     = findData.ftLastWriteTime,
                                }
                            }
                        };

                        if ((findData.dwFileAttributes & FileAttributes.Directory) != 0)
                        {
                            cloudEntry.Flags |= CF_PLACEHOLDER_CREATE_FLAGS.CF_PLACEHOLDER_CREATE_FLAG_DISABLE_ON_DEMAND_POPULATION;
                            cloudEntry.FsMetadata.FileSize = 0;
                        }

                        try
                        {
                            Console.Write("Creating placeholder for {0}\n", relativeName);
                            CfCreatePlaceholders(destPath, new[] { cloudEntry }, 1, CF_CREATE_FLAGS.CF_CREATE_FLAG_NONE, out _).ThrowIfFailed();
                        }
                        catch (Exception ex)
                        {
                            // to_hresult() will eat the exception if it is a result of check_hresult,
                            // otherwise the exception will get rethrown and this method will crash out as it should
                            Console.Write("Failed to create placeholder for {0} with {1:X8}\n", relativeName, ex.HResult);
                            // Eating it here lets other files still get a chance. Not worth crashing the sample, but
                            // certainly noteworthy for production code
                            continue;
                        }

                        try
                        {
                            var prop = new StorageProviderItemProperty
                            {
                                Id    = 1,
                                Value = "Value1",
                                // This icon is just for the sample. You should provide your own branded icon here
                                IconResource = "shell32.dll,-44"
                            };

                            Console.Write("Applying custom state for {0}\n", relativeName);
                            Utilities.ApplyCustomStateToPlaceholderFile(destPath, relativeName, prop);

                            if ((findData.dwFileAttributes & FileAttributes.Directory) != 0)
                            {
                                Create(sourcePathStr, relativeName, destPath);
                            }
                        }
                        catch (Exception ex)
                        {
                            // to_hresult() will eat the exception if it is a result of check_hresult,
                            // otherwise the exception will get rethrown and this method will crash out as it should
                            Console.Write("Failed to set custom state on {0} with {1:X8}\n", relativeName, ex.HResult);
                            // Eating it here lets other files still get a chance. Not worth crashing the sample, but
                            // certainly noteworthy for production code
                        }
                    } while (FindNextFile(hFileHandle, out findData));
                }
            }
            catch (Exception ex)
            {
                Console.Write("Could not create cloud file placeholders in the sync root with {0:X8}\n", ex.HResult);
                // Something weird enough happened that this is worth crashing out
                throw;
            }
        }