コード例 #1
0
        /// <summary>
        /// Moves an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">The store item to move.</param>
        /// <param name="destinationName">The
        /// <see cref="IWebDavStoreItem" /> that refers to the item that was moved,
        /// in its new location.</param>
        /// <returns>
        /// The moved <see cref="IWebDavStoreItem" /> instance.
        /// </returns>
        /// <exception cref="System.Exception">Path to the source item not defined.</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        /// <remarks>
        /// Note that the method should fail without creating or overwriting content in the
        /// target collection if the move cannot go through.
        /// </remarks>
        public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName)
        {
            // try to get the path of the source item
            string sourceItemPath          = "";
            WebDavDiskStoreItem sourceItem = (WebDavDiskStoreItem)source;

            sourceItemPath = sourceItem.ItemPath;

            if (sourceItemPath.Equals(""))
            {
                throw new Exception("Path to the source item not defined.");
            }

            // We get the path of
            string destinationItemPath = Path.Combine(ItemPath, destinationName);

            // the moving of the item
            if (source.IsCollection)
            {
                try
                {
                    // Impersonate the current user and move the directory
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    Directory.Move(sourceItemPath, destinationItemPath);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }

                // We return the moved file as a WebDAVDiskStoreDocument
                var collection = new WebDavDiskStoreCollection(this, destinationItemPath);
                _items.Add(destinationName, new WeakReference(collection));
                return(collection);
            }

            try
            {
                // Impersonate the current user and move the file
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Move(sourceItemPath, destinationItemPath);
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            // We return the moved file as a WebDAVDiskStoreDocument
            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, destinationItemPath);

            _items.Add(destinationName, new WeakReference(document));
            return(document);
        }
コード例 #2
0
        /// <summary>
        /// Deletes a store item by its name.
        /// </summary>
        /// <param name="item">The name of the store item to delete.</param>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavNotFoundException">If the item was not found.</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access.</exception>
        public void Delete(IWebDavStoreItem item)
        {
            WebDavDiskStoreItem diskItem = (WebDavDiskStoreItem)item;
            string itemPath = diskItem.ItemPath;

            if (item is WebDavDiskStoreDocument)
            {
                if (!File.Exists(itemPath))
                {
                    throw new WebDavNotFoundException();
                }
                try
                {
                    // Impersonate the current user and delete the file
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    File.Delete(itemPath);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }
            }
            else
            {
                if (!Directory.Exists(itemPath))
                {
                    throw new WebDavNotFoundException();
                }
                try
                {
                    // Impersonate the current user and delete the directory
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    Directory.Delete(diskItem.ItemPath);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }
            }
        }