private async void DeleteFileButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string filename = file.Name;
                    await file.DeleteAsync();

                    rootPage.sampleFile = null;
                    rootPage.NotifyUser($"The file '{filename}' was deleted", NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser($"Error deleting file '{file.Name}': {ex.Message}", NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
예제 #2
0
        private async void CopyFileButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    StorageFolder picturesLibrary = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.PicturesLibrary);

                    StorageFile fileCopy = await file.CopyAsync(picturesLibrary, "sample - Copy.dat", NameCollisionOption.ReplaceExisting);

                    rootPage.NotifyUser(String.Format("The file '{0}' was copied and the new file was named '{1}'.", file.Name, fileCopy.Name), NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser(String.Format("Error copying file '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void WriteToStreamButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string userContent = InputTextBox.Text;
                    using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
                    {
                        IBuffer buffer = MainPage.GetBufferFromString(userContent);
                        await transaction.Stream.WriteAsync(buffer);

                        transaction.Stream.Size = buffer.Length; // truncate file
                        await transaction.CommitAsync();

                        rootPage.NotifyUser($"The following text was written to '{file.Name}' using a stream:\n{userContent}", NotifyType.StatusMessage);
                    }
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser(String.Format("Error writing to '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void WriteBytesButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string  userContent = InputTextBox.Text;
                    IBuffer buffer      = MainPage.GetBufferFromString(userContent);
                    await FileIO.WriteBufferAsync(file, buffer);

                    rootPage.NotifyUser(String.Format("The following {0} bytes of text were written to '{1}':{2}{3}", buffer.Length, file.Name, Environment.NewLine, userContent), NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser(String.Format("Error writing to '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void WriteTextButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string userContent = InputTextBox.Text;
                    if (!String.IsNullOrEmpty(userContent))
                    {
                        await FileIO.WriteTextAsync(file, userContent);

                        rootPage.NotifyUser(String.Format("The following text was written to '{0}':{1}{2}", file.Name, Environment.NewLine, userContent), NotifyType.StatusMessage);
                    }
                    else
                    {
                        rootPage.NotifyUser("The text box is empty, please write something and then click 'Write' again.", NotifyType.ErrorMessage);
                    }
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
예제 #6
0
        private async void WriteTextButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string userContent = InputTextBox.Text;
                    await FileIO.WriteTextAsync(file, userContent);

                    rootPage.NotifyUser($"The following text was written to '{file.Name}':\n{userContent}", NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser($"Error writing to '{file.Name}': {ex.Message}", NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void GetParent_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    StorageFolder parentFolder = await file.GetParentAsync();

                    if (parentFolder != null)
                    {
                        StringBuilder outputText = new StringBuilder();
                        outputText.AppendLine($"Item: {file.Name} ({file.Path})");
                        outputText.Append($"Parent: {parentFolder.Name} ({parentFolder.Path})");

                        rootPage.NotifyUser(outputText.ToString(), NotifyType.StatusMessage);
                    }
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void ShowPropertiesButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    // Get top level file properties
                    StringBuilder outputText = new StringBuilder();
                    outputText.AppendLine(String.Format("File name: {0}", file.Name));
                    outputText.AppendLine(String.Format("File type: {0}", file.FileType));

                    // Get basic properties
                    BasicProperties basicProperties = await file.GetBasicPropertiesAsync();

                    outputText.AppendLine(String.Format("File size: {0} bytes", basicProperties.Size));
                    outputText.AppendLine(String.Format("Date modified: {0}", basicProperties.DateModified));

                    // Get extra properties
                    List <string> propertiesName = new List <string>();
                    propertiesName.Add(dateAccessedProperty);
                    propertiesName.Add(fileOwnerProperty);
                    IDictionary <string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);

                    var propValue = extraProperties[dateAccessedProperty];
                    if (propValue != null)
                    {
                        outputText.AppendLine(String.Format("Date accessed: {0}", propValue));
                    }
                    propValue = extraProperties[fileOwnerProperty];
                    if (propValue != null)
                    {
                        outputText.Append(String.Format("File owner: {0}", propValue));
                    }

                    rootPage.NotifyUser(outputText.ToString(), NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser(String.Format("Error retrieving properties for '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
예제 #9
0
        private void AddToListButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                if (MRURadioButton.IsChecked.Value)
                {
                    // Add the file to app MRU and possibly system MRU
                    RecentStorageItemVisibility visibility = SystemMRUCheckBox.IsChecked.Value ? RecentStorageItemVisibility.AppAndSystem : RecentStorageItemVisibility.AppOnly;
                    rootPage.mruToken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, file.Name, visibility);
                    rootPage.NotifyUser(String.Format("The file '{0}' was added to the MRU list and a token was stored.", file.Name), NotifyType.StatusMessage);
                }
                else
                {
                    try
                    {
                        rootPage.falToken = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name);
                        rootPage.NotifyUser(String.Format("The file '{0}' was added to the FAL list and a token was stored.", file.Name), NotifyType.StatusMessage);
                    }
                    catch (Exception ex) when(ex.HResult == FA_E_MAX_PERSISTED_ITEMS_REACHED)
                    {
                        // A real program would call Remove() to create room in the FAL.
                        rootPage.NotifyUser(String.Format("The file '{0}' was not added to the FAL list because the FAL list is full.", file.Name), NotifyType.ErrorMessage);
                    }
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void WriteToStreamButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string userContent = InputTextBox.Text;
                    if (!String.IsNullOrEmpty(userContent))
                    {
                        using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
                        {
                            using (DataWriter dataWriter = new DataWriter(transaction.Stream))
                            {
                                dataWriter.WriteString(userContent);
                                transaction.Stream.Size = await dataWriter.StoreAsync(); // reset stream size to override the file

                                await transaction.CommitAsync();

                                rootPage.NotifyUser(String.Format("The following text was written to '{0}' using a stream:{1}{2}", file.Name, Environment.NewLine, userContent), NotifyType.StatusMessage);
                            }
                        }
                    }
                    else
                    {
                        rootPage.NotifyUser("The text box is empty, please write something and then click 'Write' again.", NotifyType.ErrorMessage);
                    }
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
                catch (Exception ex)
                {
                    // I/O errors are reported as exceptions.
                    rootPage.NotifyUser(String.Format("Error writing to '{0}': {1}", file.Name, ex.Message), NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void CompareFilesButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                FileOpenPicker picker = new FileOpenPicker();
                picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                picker.FileTypeFilter.Add("*");
                StorageFile comparand = await picker.PickSingleFileAsync();

                if (comparand != null)
                {
                    try
                    {
                        if (file.IsEqual(comparand))
                        {
                            rootPage.NotifyUser("Files are equal", NotifyType.StatusMessage);
                        }
                        else
                        {
                            rootPage.NotifyUser("Files are not equal", NotifyType.StatusMessage);
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        rootPage.NotifyUserFileNotExist();
                    }
                }
                else
                {
                    rootPage.NotifyUser("Operation cancelled", NotifyType.StatusMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void CopyFileButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    StorageFile fileCopy = await file.CopyAsync(KnownFolders.PicturesLibrary, "sample - Copy.dat", NameCollisionOption.ReplaceExisting);

                    rootPage.NotifyUser(String.Format("The file '{0}' was copied and the new file was named '{1}'.", file.Name, fileCopy.Name), NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
예제 #13
0
        private void AddToListButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                if (MRURadioButton.IsChecked.Value)
                {
                    // Add the file to app MRU and possibly system MRU
                    RecentStorageItemVisibility visibility = SystemMRUCheckBox.IsChecked.Value ? RecentStorageItemVisibility.AppAndSystem : RecentStorageItemVisibility.AppOnly;
                    rootPage.mruToken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, file.Name, visibility);
                    rootPage.NotifyUser(String.Format("The file '{0}' was added to the MRU list and a token was stored.", file.Name), NotifyType.StatusMessage);
                }
                else if (FALRadioButton.IsChecked.Value)
                {
                    rootPage.falToken = StorageApplicationPermissions.FutureAccessList.Add(file, file.Name);
                    rootPage.NotifyUser(String.Format("The file '{0}' was added to the FAL list and a token was stored.", file.Name), NotifyType.StatusMessage);
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }
        private async void DeleteFileButton_Click(object sender, RoutedEventArgs e)
        {
            StorageFile file = rootPage.sampleFile;

            if (file != null)
            {
                try
                {
                    string filename = file.Name;
                    await file.DeleteAsync();

                    rootPage.sampleFile = null;
                    rootPage.NotifyUser(String.Format("The file '{0}' was deleted", filename), NotifyType.StatusMessage);
                }
                catch (FileNotFoundException)
                {
                    rootPage.NotifyUserFileNotExist();
                }
            }
            else
            {
                rootPage.NotifyUserFileNotExist();
            }
        }