From 052cd84a5ba940177f0b5cd3dbab4176e57e236e Mon Sep 17 00:00:00 2001 From: Adrian Soundy Date: Mon, 22 Apr 2019 11:04:05 +1200 Subject: [PATCH] Implement new methods Adde suport for - StorageFolder - Delete - Rename - GetFolder StorageFile - Delete - Rename --- source/Windows.Storage/IStorageItem.cs | 7 ++- source/Windows.Storage/StorageFile.cs | 56 +++++++++++++++-- source/Windows.Storage/StorageFolder.cs | 82 +++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 12 deletions(-) diff --git a/source/Windows.Storage/IStorageItem.cs b/source/Windows.Storage/IStorageItem.cs index cb931bb..4b45cb6 100644 --- a/source/Windows.Storage/IStorageItem.cs +++ b/source/Windows.Storage/IStorageItem.cs @@ -46,7 +46,12 @@ public interface IStorageItem // public bool IsOfType(StorageItemTypes type); - // public IAsyncAction RenameAsync(String desiredName); + + /// + /// Renames the current item. + /// + /// The desired, new name of the item. + void Rename(String desiredName); // public IAsyncAction RenameAsync(String desiredName, NameCollisionOption option); diff --git a/source/Windows.Storage/StorageFile.cs b/source/Windows.Storage/StorageFile.cs index 2abdc02..0d6ce82 100644 --- a/source/Windows.Storage/StorageFile.cs +++ b/source/Windows.Storage/StorageFile.cs @@ -145,9 +145,23 @@ public string FileType // public static IAsyncOperation CreateStreamedFileFromUriAsync(String displayNameWithExtension, Uri uri, IRandomAccessStreamReference thumbnail) // { } - // public IAsyncAction DeleteAsync() - // { } - + /// + /// Delete the current file. + /// + /// + /// + /// If the file doesn't exist this method will throw an exception. + /// + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be DeleteAsync(). + /// + /// + public void Delete() + { + DeleteFileNative(); + } + // public IAsyncAction DeleteAsync(StorageDeleteOption option) // { } @@ -239,8 +253,30 @@ public static StorageFile GetFileFromPath(String path) // public IAsyncOperation OpenTransactedWriteAsync(StorageOpenOptions options) // { } - // public IAsyncAction RenameAsync(String desiredName) - // { } + /// + /// Renames the current file. + /// + /// The desired new name of the current file. + /// + /// + /// If the name you specify is invalid or already exists, this method throws an exception + /// + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be RenameAsync(String desiredName). + /// + /// + public void Rename(string desiredName) + { + // Construct path for new name + string desiredPath = _path.Substring(0, _path.Length - _name.Length) + desiredName; + + RenameFileNative(desiredPath); + + // No exception, so update file Path & name + _path = desiredPath; + _name = desiredName; + } // public IAsyncAction RenameAsync(String desiredName, NameCollisionOption option) // { } @@ -257,5 +293,15 @@ public static StorageFile GetFileFromPath(String path) [MethodImpl(MethodImplOptions.InternalCall)] private static extern void CheckFileNative(string filePath); + [System.Diagnostics.DebuggerStepThrough] + [System.Diagnostics.DebuggerHidden] + [MethodImpl(MethodImplOptions.InternalCall)] + private extern void DeleteFileNative(); + + [System.Diagnostics.DebuggerStepThrough] + [System.Diagnostics.DebuggerHidden] + [MethodImpl(MethodImplOptions.InternalCall)] + private extern void RenameFileNative(String desiredName); + } } diff --git a/source/Windows.Storage/StorageFolder.cs b/source/Windows.Storage/StorageFolder.cs index 544d20f..cfd2105 100644 --- a/source/Windows.Storage/StorageFolder.cs +++ b/source/Windows.Storage/StorageFolder.cs @@ -34,10 +34,10 @@ public sealed class StorageFolder : IStorageFolder//, IStorageFolder2, IStorageI #pragma warning restore 0649 [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] - private readonly string _name; + private string _name; [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)] - private readonly string _path; + private string _path; #endregion @@ -195,8 +195,10 @@ public StorageFolder CreateFolder(String desiredName) /// When this method completes, it returns a that represents the new subfolder. /// /// + /// /// This method is exclusive of nanoFramework and it's not available in the UWP API. /// The equivalent method would be CreateFolderAsync(String, CreationCollisionOption). + /// /// public StorageFolder CreateFolder(String desiredName, CreationCollisionOption options) { @@ -218,8 +220,21 @@ public StorageFolder CreateFolder(String desiredName, CreationCollisionOption op // public StorageItemQueryResult CreateItemQueryWithOptions(QueryOptions queryOptions) // { } - // public IAsyncAction DeleteAsync() - // { } + /// + /// Delete the current folder. + /// + /// + /// + /// If the folder doesn't exist then this method will throw an exception. + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be DeleteAsync(). + /// + /// + public void Delete() + { + DeleteFolderNative(); + } // public IAsyncAction DeleteAsync(StorageDeleteOption option) // { } @@ -296,6 +311,27 @@ public StorageFile[] GetFiles(CommonFileQuery query, UInt32 startIndex, UInt32 m return GetStorageFilesNative(startIndex, maxItemsToRetrieve); } + /// + /// Gets the subfolder with the specified name from the current folder. + /// + /// The name (or path relative to the current folder) of the subfolder to get. + /// + /// When this method completes successfully, it returns a StorageFolder that represents the specified subfolder. + /// + /// + /// + /// If the folder doesn't exist it will throw an exception. + /// + /// + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be GetFolderAsync(String name). + /// + /// + public StorageFolder GetFolder(String name) + { + return GetFolderNative(name); + } + // public IAsyncOperation GetFolderAsync(String name) // { } @@ -395,8 +431,28 @@ public bool IsCommonFileQuerySupported(CommonFileQuery query) // public bool IsOfType(StorageItemTypes type) // { } - // public IAsyncAction RenameAsync(String desiredName) - // { } + /// + /// Renames the current folder. + /// + /// The desired, new name for the current folder. + /// + /// If the name you specify is invalid or a folder with the same name already exists, + /// this method throws an exception. If the target device doesn't support folders then this will also + /// throw an exception. + /// This method is exclusive of nanoFramework and it's not available in the UWP API. + /// The equivalent method would be RenameAsync(String desiredName). + /// + public void Rename(String desiredName) + { + // Create path to disired folder + string desiredPath = _path.Substring(0, _path.Length - _name.Length) + desiredName; + + RenameFolderNative(desiredPath); + + // No exception, so update folder Path & name + _path = desiredPath; + _name = desiredName; + } // public IAsyncAction RenameAsync(String desiredName, NameCollisionOption option) // { } @@ -439,6 +495,20 @@ public bool IsCommonFileQuerySupported(CommonFileQuery query) [MethodImpl(MethodImplOptions.InternalCall)] private extern StorageFolder CreateFolderNative(string desiredName, uint options); + [System.Diagnostics.DebuggerStepThrough] + [System.Diagnostics.DebuggerHidden] + [MethodImpl(MethodImplOptions.InternalCall)] + private extern void DeleteFolderNative(); + + [System.Diagnostics.DebuggerStepThrough] + [System.Diagnostics.DebuggerHidden] + [MethodImpl(MethodImplOptions.InternalCall)] + private extern void RenameFolderNative(String desiredPath); + + [System.Diagnostics.DebuggerStepThrough] + [System.Diagnostics.DebuggerHidden] + [MethodImpl(MethodImplOptions.InternalCall)] + private extern StorageFolder GetFolderNative(String name); #endregion }