Esempio n. 1
0
        public static async Task <bool> Copy(this IFile src, IFileSystem destFileSystem, string destPath)
        {
#if PORTABLE
            return(false);
#else
            IFile           dest      = null;
            var             r         = false;
            LocalFileAccess srcLocal  = null;
            LocalFileAccess destLocal = null;

            try {
                dest = await destFileSystem.CreateFile(destPath, "");

                srcLocal = await src.BeginLocalAccess();

                destLocal = await dest.BeginLocalAccess();

                var srcLocalPath  = srcLocal.LocalPath;
                var destLocalPath = destLocal.LocalPath;

                System.IO.File.Copy(srcLocalPath, destLocalPath, overwrite: true);

                r = true;
            } catch (Exception ex) {
                Debug.WriteLine(ex);
                r = false;
            }

            if (srcLocal != null)
            {
                await srcLocal.End();
            }
            if (destLocal != null)
            {
                await destLocal.End();
            }

            return(r);

//			await Task.Factory.StartNew (() => {
//
//				var fc = new NSFileCoordinator (filePresenterOrNil: null);
//				NSError coordErr;
//
//				fc.CoordinateReadWrite (
//					srcPath, NSFileCoordinatorReadingOptions.WithoutChanges,
//					destPath, NSFileCoordinatorWritingOptions.ForReplacing,
//					out coordErr, (readUrl, writeUrl) => {
//
//					var r = false;
//					try {
//						File.Copy (readUrl.Path, writeUrl.Path, overwrite: true);
//						r = true;
//					} catch (Exception) {
//						r = false;
//					}
//					tcs.SetResult (r);
//				});
//			});
#endif
        }
Esempio n. 2
0
		public async Task Close ()
		{
			if (Document == null)
				throw new InvalidOperationException ("Trying to Close an unopened document");

			await Document.CloseAsync ();
			Document = null;

			if (local != null) {
				await local.End ();
				local = null;
			}
		}
Esempio n. 3
0
		public async Task<DocumentReference> Duplicate (IFileSystem fs)
		{
			var baseName = Name;
			if (!baseName.EndsWith ("Copy", StringComparison.Ordinal)) {
				baseName = baseName + " Copy";
			}

			var directory = Path.GetDirectoryName (File.Path);
			LocalFileAccess local = null;
			var contents = "";
			try {
				local = await File.BeginLocalAccess ();
				var localPath = local.LocalPath;
				contents = System.IO.File.ReadAllText (localPath);
			} catch (Exception ex) {
				Debug.WriteLine (ex);
			}
			if (local != null)
				await local.End ();

			var ext = Path.GetExtension (File.Path);

			var dr = await New (directory, baseName, ext, fs, dctor, contents);

			dr.IsNew = false;

			return dr;
		}
Esempio n. 4
0
		public async Task<IDocument> Open ()
		{
			if (Document != null)
				throw new InvalidOperationException ("Cannot Open already opened document");

			if (local != null)
				throw new InvalidOperationException ("Cannot Open already locally accessed document");

			local = await File.BeginLocalAccess ();

			try {
				var doc = dctor (LocalFilePath);
				if (doc == null)
					throw new ApplicationException ("CreateDocument must return a document");

				if (!System.IO.File.Exists (LocalFilePath)) {
					Debug.WriteLine ("CREATE " + LocalFilePath);
					await doc.SaveAsync (LocalFilePath, DocumentSaveOperation.ForCreating);
				}
				else {
					Debug.WriteLine ("OPEN " + LocalFilePath);
					await doc.OpenAsync ();
				}
				Document = doc;

			} catch (Exception ex) {
				Document = null;
				Debug.WriteLine (ex);
			}

			return Document;
		}