/// <summary>
		/// Moves a file from a given source url location to a given destination url.
		/// </summary>
		/// <returns><c>true</c>, if file was moved, <c>false</c> otherwise.</returns>
		/// <param name="fromURL">From UR.</param>
		/// <param name="toURL">To UR.</param>
		private bool MoveFile(string fromURL, string toURL) {
			bool successful = true;

			// Get source options
			var srcURL = NSUrl.FromFilename (fromURL);
			var srcIntent = NSFileAccessIntent.CreateReadingIntent (srcURL, NSFileCoordinatorReadingOptions.ForUploading);

			// Get destination options
			var dstURL = NSUrl.FromFilename (toURL);
			var dstIntent = NSFileAccessIntent.CreateReadingIntent (dstURL, NSFileCoordinatorReadingOptions.ForUploading);

			// Create an array
			var intents = new NSFileAccessIntent[] {
				srcIntent,
				dstIntent
			};

			// Initialize a file coordination with intents
			var queue = new NSOperationQueue ();
			var fileCoordinator = new NSFileCoordinator ();
			fileCoordinator.CoordinateAccess (intents, queue, (err) => {
				// Was there an error?
				if (err!=null) {
					// Yes, inform caller
					Console.WriteLine("Error: {0}",err.LocalizedDescription);
					successful = false;
				}
			});

			// Return successful
			return successful;
		}
Пример #2
0
        /// <summary>
        /// Moves a file from a given source url location to a given destination url.
        /// </summary>
        /// <returns><c>true</c>, if file was moved, <c>false</c> otherwise.</returns>
        /// <param name="fromURL">From UR.</param>
        /// <param name="toURL">To UR.</param>
        private bool MoveFile(string fromURL, string toURL)
        {
            bool successful = true;

            // Get source options
            var srcURL    = NSUrl.FromFilename(fromURL);
            var srcIntent = NSFileAccessIntent.CreateReadingIntent(srcURL, NSFileCoordinatorReadingOptions.ForUploading);

            // Get destination options
            var dstURL    = NSUrl.FromFilename(toURL);
            var dstIntent = NSFileAccessIntent.CreateReadingIntent(dstURL, NSFileCoordinatorReadingOptions.ForUploading);

            // Create an array
            var intents = new NSFileAccessIntent[] {
                srcIntent,
                dstIntent
            };

            // Initialize a file coordination with intents
            var queue           = new NSOperationQueue();
            var fileCoordinator = new NSFileCoordinator();

            fileCoordinator.CoordinateAccess(intents, queue, (err) => {
                // Was there an error?
                if (err != null)
                {
                    // Yes, inform caller
                    Console.WriteLine("Error: {0}", err.LocalizedDescription);
                    successful = false;
                }
            });

            // Return successful
            return(successful);
        }
Пример #3
0
            public override async Task DeleteAsync(CancellationToken ct, StorageDeleteOption options)
            {
                var intent = NSFileAccessIntent.CreateWritingIntent(_nsUrl, NSFileCoordinatorWritingOptions.ForDeleting);

                using var coordinator = new NSFileCoordinator();
                await coordinator.CoordinateAsync(new[] { intent }, new NSOperationQueue(), () =>
                {
                    using var _ = _nsUrl.BeginSecurityScopedAccess();
                    NSError deleteError;

                    NSFileManager.DefaultManager.Remove(_nsUrl, out deleteError);

                    if (deleteError != null)
                    {
                        throw new UnauthorizedAccessException($"Can't delete file. {deleteError}");
                    }
                });
            }
Пример #4
0
        public static async Task <FileResult[]> EnsurePhysicalFileResultsAsync(params NSUrl[] urls)
        {
            if (urls == null || urls.Length == 0)
            {
                return(Array.Empty <FileResult>());
            }

            var opts    = NSFileCoordinatorReadingOptions.WithoutChanges;
            var intents = urls.Select(x => NSFileAccessIntent.CreateReadingIntent(x, opts)).ToArray();

            using var coordinator = new NSFileCoordinator();

            var tcs = new TaskCompletionSource <FileResult[]>();

            coordinator.CoordinateAccess(intents, new NSOperationQueue(), error =>
            {
                if (error != null)
                {
                    tcs.TrySetException(new NSErrorException(error));
                    return;
                }

                var bookmarks = new List <FileResult>();

                foreach (var intent in intents)
                {
                    var url    = intent.Url;
                    var result = new BookmarkDataFileResult(url);
                    bookmarks.Add(result);
                }

                tcs.TrySetResult(bookmarks.ToArray());
            });

            return(await tcs.Task);
        }