private static string Publish(DiskSdkClient client, string remotePath)
        {
            var sync = new SyncObject();

            Exception error  = null;
            string    result = null;

            EventHandler <GenericSdkEventArgs <string> > handler = (s, e) =>
            {
                if (e.Error == null)
                {
                    result = e.Result;
                }
                else
                {
                    error = e.Error;
                }

                sync.Pulse();
            };

            client.PublishCompleted += handler;
            client.PublishAsync(remotePath);

            sync.Wait();
            client.PublishCompleted -= handler;

            if (error != null)
            {
                error.Throw();
            }

            return(result);
        }
        private static void TryCreateDirectory(DiskSdkClient client, string path)
        {
            var sync  = new SyncObject();
            var items = Enumerable.Empty <DiskItemInfo>();

            Exception error = null;

            EventHandler <GenericSdkEventArgs <IEnumerable <DiskItemInfo> > > listHandler = (s, e) =>
            {
                if (e.Error != null)
                {
                    error = e.Error;
                }
                else
                {
                    items = e.Result;
                }

                sync.Pulse();
            };

            client.GetListCompleted += listHandler;
            client.GetListAsync();

            sync.Wait();
            client.GetListCompleted -= listHandler;

            if (error != null)
            {
                throw error;
            }

            if (items.Any(i => i.IsDirectory && i.OriginalFullPath.TrimEnd("/") == path))
            {
                return;
            }

            EventHandler <SdkEventArgs> createHandler = (s, e) =>
            {
                error = e.Error;
                sync.Pulse();
            };

            client.MakeFolderCompleted += createHandler;
            client.MakeDirectoryAsync(path);

            sync.Wait();
            client.MakeFolderCompleted -= createHandler;

            if (error != null)
            {
                throw error;
            }
        }
        private static T Do <T>(string file, Window owner, Func <DiskSdkClient, string, T> action)
        {
            if (file.IsEmpty())
            {
                throw new ArgumentNullException(nameof(file));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (!File.Exists(file))
            {
                throw new FileNotFoundException(LocalizedStrings.Str1575, file);
            }

            Exception error  = null;
            var       result = default(T);

            var loginWindow = new YandexLoginWindow();

            loginWindow.AuthCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    var client = new DiskSdkClient(e.Result);

                    var remotePath = RootPath + "/" + Path.GetFileName(file);

                    try
                    {
                        result = action(client, remotePath);
                    }
                    catch (Exception excp)
                    {
                        error = excp;
                    }
                }
                else
                {
                    error = e.Error;
                }
            };
            loginWindow.ShowModal(owner);

            if (error != null)
            {
                error.Throw();
            }

            return(result);
        }
        /// <summary>
        /// Поделиться файлом.
        /// </summary>
        /// <param name="file">Файл.</param>
        /// <returns>Ссылка на файл.</returns>
        public static string Publish(string file)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (!File.Exists(file))
            {
                throw new FileNotFoundException(LocalizedStrings.Str1575, file);
            }

            Exception error  = null;
            String    result = null;

            var loginWindow = new YandexLoginWindow();

            loginWindow.AuthCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    var client = new DiskSdkClient(e.Result);

                    var remoteDir  = RootPath;
                    var remotePath = remoteDir + "/" + Path.GetFileName(file);

                    try
                    {
                        TryCreateDirectory(client, remoteDir);
                        UploadFile(client, remotePath, file);
                        result = Publish(client, remotePath);
                    }
                    catch (Exception excp)
                    {
                        error = excp;
                    }
                }
                else
                {
                    error = e.Error;
                }
            };
            loginWindow.ShowDialog();

            if (error != null)
            {
                throw error;
            }

            return(result);
        }
        private static void UploadFile(DiskSdkClient client, string remotePath, string localPath)
        {
            var       sync  = new SyncObject();
            Exception error = null;

            client.UploadFileAsync(remotePath, File.OpenRead(localPath),
                                   new AsyncProgress((c, t) => { }),
                                   (us, ua) =>
            {
                error = ua.Error;
                sync.Pulse();
            });

            sync.Wait();

            if (error != null)
            {
                error.Throw();
            }
        }
		/// <summary>
		/// To share a file.
		/// </summary>
		/// <param name="file">File.</param>
		/// <returns>The link to a file.</returns>
		public static string Publish(string file)
		{
			if (file == null)
				throw new ArgumentNullException("file");

			if (!File.Exists(file))
				throw new FileNotFoundException(LocalizedStrings.Str1575, file);

			Exception error = null;
			String result = null;

			var loginWindow = new YandexLoginWindow();
			loginWindow.AuthCompleted += (s, e) =>
			{
				if (e.Error == null)
				{
					var client = new DiskSdkClient(e.Result);

					var remoteDir = RootPath;
					var remotePath = remoteDir + "/" + Path.GetFileName(file);

					try
					{
						TryCreateDirectory(client, remoteDir);
						UploadFile(client, remotePath, file);
						result = Publish(client, remotePath);
					}
					catch (Exception excp)
					{
						error = excp;
					}
				}
				else
					error = e.Error;
			};
			loginWindow.ShowDialog();

			if (error != null)
				throw error;

			return result;
		}
		private static string Publish(DiskSdkClient client, string remotePath)
		{
			var sync = new SyncObject();

			Exception error = null;
			String result = null;

			EventHandler<GenericSdkEventArgs<string>> handler = (s, e) =>
			{
				if (e.Error == null)
					result = e.Result;
				else
					error = e.Error;

				sync.Pulse();
			};

			client.PublishCompleted += handler;
			client.PublishAsync(remotePath);

			sync.Wait();
			client.PublishCompleted -= handler;

			if (error != null)
				throw error;

			return result;
		}
		private static void UploadFile(DiskSdkClient client, string remotePath, string localPath)
		{
			var sync = new SyncObject();
			Exception error = null;

			client.UploadFileAsync(remotePath, File.OpenRead(localPath),
				new AsyncProgress((c, t) => { }),
				(us, ua) =>
				{
					error = ua.Error;
					sync.Pulse();
				});

			sync.Wait();

			if (error != null)
				throw error;
		}
		private static void TryCreateDirectory(DiskSdkClient client, string path)
		{
			var sync = new SyncObject();
			var items = Enumerable.Empty<DiskItemInfo>();

			Exception error = null;

			EventHandler<GenericSdkEventArgs<IEnumerable<DiskItemInfo>>> listHandler = (s, e) =>
			{
				if (e.Error != null)
					error = e.Error;
				else
					items = e.Result;

				sync.Pulse();
			};

			client.GetListCompleted += listHandler;
			client.GetListAsync();

			sync.Wait();
			client.GetListCompleted -= listHandler;

			if (error != null)
				throw error;

			if (items.Any(i => i.IsDirectory && i.OriginalFullPath.TrimEnd("/") == path))
				return;

			EventHandler<SdkEventArgs> createHandler = (s, e) =>
			{
				error = e.Error;
				sync.Pulse();
			};

			client.MakeFolderCompleted += createHandler;
			client.MakeDirectoryAsync(path);

			sync.Wait();
			client.MakeFolderCompleted -= createHandler;

			if (error != null)
				throw error;
		}