コード例 #1
0
        /// <summary>
        /// Main entry point for handling a Setting operation
        /// </summary>
        /// <param name="portal">DevicePortal reference for communicating with the device.</param>
        /// <param name="parameters">Parsed command line parameters.</param>
        public static void HandleOperation(DevicePortal portal, ParameterHelper parameters)
        {
            if (parameters.HasFlag(ParameterHelper.HelpFlag))
            {
                Console.WriteLine(XblFileUsageMessage);
                return;
            }

            string operationType = parameters.GetParameterValue("subop");

            if (string.IsNullOrWhiteSpace(operationType))
            {
                Console.WriteLine("Missing subop parameter");
                Console.WriteLine();
                Console.WriteLine(XblFileUsageMessage);
                return;
            }

            operationType = operationType.ToLowerInvariant();

            string knownFolderId   = parameters.GetParameterValue("knownfolderid");
            string subPath         = parameters.GetParameterValue("subpath");
            string packageFullName = parameters.GetParameterValue("packagefullname");

            try
            {
                if (operationType.Equals("knownfolders"))
                {
                    Task <KnownFolders> getKnownFoldersTask = portal.GetKnownFoldersAsync();

                    getKnownFoldersTask.Wait();
                    Console.WriteLine(getKnownFoldersTask.Result);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(knownFolderId))
                    {
                        Console.WriteLine("Missing knownfolderid parameter");
                        Console.WriteLine();
                        Console.WriteLine(XblFileUsageMessage);
                        return;
                    }

                    if (operationType.Equals("dir"))
                    {
                        Task <FolderContents> getDirectoryContents = portal.GetFolderContentsAsync(knownFolderId, subPath, packageFullName);

                        getDirectoryContents.Wait();
                        Console.WriteLine(getDirectoryContents.Result);
                    }
                    else if (operationType.Equals("upload"))
                    {
                        string filepath = parameters.GetParameterValue("filepath");

                        if (string.IsNullOrWhiteSpace(filepath))
                        {
                            Console.WriteLine("Missing filepath parameter");
                            Console.WriteLine();
                            Console.WriteLine(XblFileUsageMessage);
                            return;
                        }

                        Task uploadFileTask = portal.UploadFileAsync(knownFolderId, filepath, subPath, packageFullName);

                        uploadFileTask.Wait();
                        Console.WriteLine(string.Format("{0} uploaded.", filepath));
                    }
                    else
                    {
                        string filename = parameters.GetParameterValue("filename");

                        if (string.IsNullOrWhiteSpace(filename))
                        {
                            Console.WriteLine("Missing filename parameter");
                            Console.WriteLine();
                            Console.WriteLine(XblFileUsageMessage);
                            return;
                        }

                        if (operationType.Equals("download"))
                        {
                            string destination = parameters.GetParameterValue("destination");

                            if (string.IsNullOrWhiteSpace(destination))
                            {
                                Console.WriteLine("Missing destination parameter");
                                Console.WriteLine();
                                Console.WriteLine(XblFileUsageMessage);
                                return;
                            }

                            destination += "/" + filename;

                            Task <Stream> getFile = portal.GetFileAsync(knownFolderId, filename, subPath, packageFullName);

                            getFile.Wait();

                            using (FileStream filestream = new FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                getFile.Result.CopyTo(filestream);
                            }

                            Console.WriteLine(string.Format("Downloaded {0}.", destination));
                        }
                        else if (operationType.Equals("rename"))
                        {
                            string newfilename = parameters.GetParameterValue("newfilename");

                            if (string.IsNullOrWhiteSpace(newfilename))
                            {
                                Console.WriteLine("Missing newfilename parameter");
                                Console.WriteLine();
                                Console.WriteLine(XblFileUsageMessage);
                                return;
                            }

                            Task renameFileTask = portal.RenameFileAsync(knownFolderId, filename, newfilename, subPath, packageFullName);

                            renameFileTask.Wait();
                            Console.WriteLine(string.Format("Renamed {0} to {1}.", filename, newfilename));
                        }
                        else if (operationType.Equals("delete"))
                        {
                            Task deleteFileTask = portal.DeleteFileAsync(knownFolderId, filename, subPath, packageFullName);

                            deleteFileTask.Wait();
                            Console.WriteLine(string.Format("Deleted {0}.", filename));
                        }
                    }
                }
            }
            catch (AggregateException e)
            {
                if (e.InnerException != null && e.InnerException is DevicePortalException)
                {
                    DevicePortalException exception = e.InnerException as DevicePortalException;

                    Console.WriteLine(string.Format("HTTP Status: {0}, Hresult: 0x{1:X8}. {2}", exception.StatusCode, exception.HResult, exception.Reason));
                }
            }
        }