Skip to content

Commit 7e8fbba

Browse files
committed
Extended PortablePath
Added BaseFile and BaseFolder Restructured FileSystem IFolder GetFiles() supports search all folders Added SpecialStorage
1 parent 46fa408 commit 7e8fbba

File tree

15 files changed

+426
-396
lines changed

15 files changed

+426
-396
lines changed

common/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
[assembly: AssemblyTrademark("")]
88
[assembly: AssemblyCulture("")]
99

10-
[assembly: AssemblyVersion("1.0.9.1")]
11-
[assembly: AssemblyFileVersion("1.0.9.1")]
10+
[assembly: AssemblyVersion("1.1.0.0")]
11+
[assembly: AssemblyFileVersion("1.1.0.0")]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace PCLExt.FileStorage
2+
{
3+
public enum FolderSearchOption
4+
{
5+
TopFolderOnly,
6+
AllFolders,
7+
}
8+
}

src/PCLExt.FileStorage.Abstractions/IFile.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public interface IFile
4848
/// <param name="fileAccess">Specifies whether the file should be opened in read-only or read/write mode</param>
4949
/// <returns>A <see cref="Stream"/> which can be used to read from or write to the file</returns>
5050
Stream Open(FileAccess fileAccess);
51-
5251
/// <summary>
5352
/// Opens the file
5453
/// </summary>
@@ -64,7 +63,6 @@ public interface IFile
6463
/// A task which will complete after the file is deleted.
6564
/// </returns>
6665
void Delete();
67-
6866
/// <summary>
6967
/// Deletes the file
7068
/// </summary>
@@ -83,7 +81,6 @@ public interface IFile
8381
/// A task which will complete after the file is renamed.
8482
/// </returns>
8583
void Rename(string newName, NameCollisionOption collisionOption = NameCollisionOption.FailIfExists);
86-
8784
/// <summary>
8885
/// Renames a file without changing its location.
8986
/// </summary>
@@ -101,7 +98,6 @@ public interface IFile
10198
/// <param name="collisionOption">How to deal with collisions with existing files.</param>
10299
/// <returns>A task which will complete after the file is moved.</returns>
103100
void Move(string newPath, NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting);
104-
105101
/// <summary>
106102
/// Moves a file.
107103
/// </summary>
@@ -118,7 +114,6 @@ public interface IFile
118114
/// <param name="collisionOption">How to deal with collisions with existing files.</param>
119115
/// <returns>A task which will complete after the file is moved.</returns>
120116
void Copy(string newPath, NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting);
121-
122117
/// <summary>
123118
/// Copies a file.
124119
/// </summary>

src/PCLExt.FileStorage.Abstractions/IFileSystem.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
// Which is released under the MS-PL license.
88
//-----------------------------------------------------------------------
99

10-
using System.Threading;
11-
using System.Threading.Tasks;
12-
1310
namespace PCLExt.FileStorage
1411
{
1512
/// <summary>
@@ -29,35 +26,9 @@ public interface IFileSystem
2926
/// A folder representing storage which may be synced with other devices for the same user.
3027
/// </summary>
3128
IFolder RoamingStorage { get; }
32-
33-
/// <summary>
34-
/// Gets a file, given its path. Returns null if the file does not exist.
35-
/// </summary>
36-
/// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
37-
/// <returns>A file for the given path, or null if it does not exist.</returns>
38-
IFile GetFileFromPath(string path);
39-
40-
/// <summary>
41-
/// Gets a file, given its path. Returns null if the file does not exist.
42-
/// </summary>
43-
/// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
44-
/// <param name="cancellationToken">The cancellation token.</param>
45-
/// <returns>A file for the given path, or null if it does not exist.</returns>
46-
Task<IFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
47-
48-
/// <summary>
49-
/// Gets a folder, given its path. Returns null if the folder does not exist.
50-
/// </summary>
51-
/// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
52-
/// <returns>A folder for the specified path, or null if it does not exist.</returns>
53-
IFolder GetFolderFromPath(string path);
54-
5529
/// <summary>
56-
/// Gets a folder, given its path. Returns null if the folder does not exist.
30+
/// Depending on OS, it will return BaseStorage on Desktop platforms and LocalStorage on Mobile platforms.
5731
/// </summary>
58-
/// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
59-
/// <param name="cancellationToken">The cancellation token.</param>
60-
/// <returns>A folder for the specified path, or null if it does not exist.</returns>
61-
Task<IFolder> GetFolderFromPathAsync(string path, CancellationToken cancellationToken = default(CancellationToken));
32+
IFolder SpecialStorage { get; }
6233
}
6334
}

src/PCLExt.FileStorage.Abstractions/IFolder.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public interface IFolder
5757
/// <param name="option">Specifies how to behave if the specified file already exists</param>
5858
/// <returns>The newly created file</returns>
5959
IFile CreateFile(string desiredName, CreationCollisionOption option);
60-
6160
/// <summary>
6261
/// Creates a file in this folder
6362
/// </summary>
@@ -73,7 +72,6 @@ public interface IFolder
7372
/// <param name="name">The name of the file to get</param>
7473
/// <returns>The requested file, or null if it does not exist</returns>
7574
IFile GetFile(string name);
76-
7775
/// <summary>
7876
/// Gets a file in this folder
7977
/// </summary>
@@ -85,15 +83,19 @@ public interface IFolder
8583
/// <summary>
8684
/// Gets a list of the files in this folder
8785
/// </summary>
86+
/// <param name="searchPattern"></param>
87+
/// <param name="searchOption"></param>
8888
/// <returns>A list of the files in the folder</returns>
89-
IList<IFile> GetFiles();
89+
IList<IFile> GetFiles(string searchPattern = "", FolderSearchOption searchOption = FolderSearchOption.TopFolderOnly);
9090

9191
/// <summary>
9292
/// Gets a list of the files in this folder
9393
/// </summary>
94+
/// <param name="searchPattern"></param>
95+
/// <param name="searchOption"></param>
9496
/// <param name="cancellationToken">The cancellation token.</param>
9597
/// <returns>A list of the files in the folder</returns>
96-
Task<IList<IFile>> GetFilesAsync(CancellationToken cancellationToken = default(CancellationToken));
98+
Task<IList<IFile>> GetFilesAsync(string searchPattern = "", FolderSearchOption searchOption = FolderSearchOption.TopFolderOnly, CancellationToken cancellationToken = default(CancellationToken));
9799

98100
/// <summary>
99101
/// Creates a subfolder in this folder
@@ -102,7 +104,6 @@ public interface IFolder
102104
/// <param name="option">Specifies how to behave if the specified folder already exists</param>
103105
/// <returns>The newly created folder</returns>
104106
IFolder CreateFolder(string desiredName, CreationCollisionOption option);
105-
106107
/// <summary>
107108
/// Creates a subfolder in this folder
108109
/// </summary>
@@ -118,7 +119,6 @@ public interface IFolder
118119
/// <param name="name">The name of the folder to get</param>
119120
/// <returns>The requested folder, or null if it does not exist</returns>
120121
IFolder GetFolder(string name);
121-
122122
/// <summary>
123123
/// Gets a subfolder in this folder
124124
/// </summary>
@@ -132,7 +132,6 @@ public interface IFolder
132132
/// </summary>
133133
/// <returns>A list of subfolders in the folder</returns>
134134
IList<IFolder> GetFolders();
135-
136135
/// <summary>
137136
/// Gets a list of subfolders in this folder
138137
/// </summary>
@@ -146,7 +145,6 @@ public interface IFolder
146145
/// <param name="name">The name of the file or folder to check for.</param>
147146
/// <returns>A task whose result is the result of the existence check.</returns>
148147
ExistenceCheckResult CheckExists(string name);
149-
150148
/// <summary>
151149
/// Checks whether a folder or file exists at the given location.
152150
/// </summary>
@@ -160,7 +158,6 @@ public interface IFolder
160158
/// </summary>
161159
/// <returns>A task which will complete after the folder is deleted</returns>
162160
void Delete();
163-
164161
/// <summary>
165162
/// Deletes this folder and all of its contents
166163
/// </summary>
@@ -175,7 +172,6 @@ public interface IFolder
175172
/// <param name="option">Specifies how to behave if the specified folder/file already exists</param>
176173
/// <returns>The folder with moved content.</returns>
177174
IFolder Move(IFolder folder, NameCollisionOption option = NameCollisionOption.ReplaceExisting);
178-
179175
/// <summary>
180176
/// Moves this folder and all of its contents to the given folder, current folder will be deleted.
181177
/// </summary>

src/PCLExt.FileStorage.Abstractions/PCLExt.FileStorage.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Compile Include="$(SolutionDir)\common\CommonAssemblyInfo.cs">
3838
<Link>Properties\CommonAssemblyInfo.cs</Link>
3939
</Compile>
40+
<Compile Include="FolderSearchOption.cs" />
4041
<Compile Include="ExistenceCheckResult.cs" />
4142
<Compile Include="FileExtensions.cs" />
4243
<Compile Include="IFile.cs" />

src/PCLExt.FileStorage.Desktop/DesktopFileSystem.cs

Lines changed: 20 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
// Which is released under the MS-PL license.
88
//-----------------------------------------------------------------------
99

10-
using System.Threading;
11-
using System.Threading.Tasks;
12-
1310
namespace PCLExt.FileStorage
1411
{
1512
/// <summary>
@@ -24,14 +21,12 @@ public class DesktopFileSystem : IFileSystem
2421
static string MacHomeDirectory => ((Foundation.NSString)ObjCRuntime.Runtime.GetNSObject(NSHomeDirectory())).ToString();
2522
#endif
2623

27-
/// <summary>
28-
/// A folder representing storage which is where the app is running.
29-
/// </summary>
30-
public IFolder BaseStorage
24+
/// <inheritdoc />
25+
public IFolder BaseStorage
3126
{
3227
get
3328
{
34-
#if ANDROID || __IOS__ || OSX
29+
#if ANDROID || __IOS__
3530
var storage = "";
3631
return null;
3732
#elif DESKTOP || MAC
@@ -41,17 +36,15 @@ public IFolder BaseStorage
4136
}
4237
}
4338

44-
/// <summary>
45-
/// A folder representing storage which is local to the current device.
46-
/// </summary>
47-
public IFolder LocalStorage
39+
/// <inheritdoc />
40+
public IFolder LocalStorage
4841
{
4942
get
5043
{
5144
#if ANDROID
5245
var storage = Android.App.Application.Context.GetExternalFilesDir(null)?.ParentFile?.AbsolutePath;
5346
if(string.IsNullOrEmpty(storage))
54-
return null;
47+
return null;
5548
#elif __IOS__
5649
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
5750
var storage = System.IO.Path.Combine(documents, "..", "Library");
@@ -73,17 +66,15 @@ public IFolder LocalStorage
7366
}
7467
}
7568

76-
/// <summary>
77-
/// A folder representing storage which may be synced with other devices for the same user.
78-
/// </summary>
79-
public IFolder RoamingStorage
69+
/// <inheritdoc />
70+
public IFolder RoamingStorage
8071
{
8172
get
8273
{
8374
#if ANDROID
8475
var storage = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
8576

86-
#elif __IOS__ || OSX || MAC
77+
#elif __IOS__ || MAC
8778
var storage = "";
8879
return null;
8980
#elif DESKTOP
@@ -93,68 +84,20 @@ public IFolder RoamingStorage
9384
}
9485
}
9586

96-
/// <summary>
97-
/// Gets a file, given its path. Returns null if the file does not exist.
98-
/// </summary>
99-
/// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
100-
/// <returns>A file for the given path, or null if it does not exist.</returns>
101-
public IFile GetFileFromPath(string path)
102-
{
103-
Requires.NotNullOrEmpty(path, "path");
104-
105-
if (System.IO.File.Exists(path))
106-
return new FileSystemFile(path);
107-
108-
return null;
109-
}
110-
111-
/// <summary>
112-
/// Gets a file, given its path. Returns null if the file does not exist.
113-
/// </summary>
114-
/// <param name="path">The path to a file, as returned from the <see cref="IFile.Path"/> property.</param>
115-
/// <param name="cancellationToken">The cancellation token.</param>
116-
/// <returns>A file for the given path, or null if it does not exist.</returns>
117-
public async Task<IFile> GetFileFromPathAsync(string path, CancellationToken cancellationToken)
118-
{
119-
Requires.NotNullOrEmpty(path, "path");
120-
121-
await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);
122-
if (System.IO.File.Exists(path))
123-
return new FileSystemFile(path);
124-
125-
return null;
126-
}
127-
128-
/// <summary>
129-
/// Gets a folder, given its path. Returns null if the folder does not exist.
130-
/// </summary>
131-
/// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
132-
/// <returns>A folder for the specified path, or null if it does not exist.</returns>
133-
public IFolder GetFolderFromPath(string path)
87+
/// <inheritdoc />
88+
public IFolder SpecialStorage
13489
{
135-
Requires.NotNullOrEmpty(path, "path");
90+
get
91+
{
92+
#if DESKTOP || MAC
93+
return BaseStorage;
13694

137-
if (System.IO.Directory.Exists(path))
138-
return new FileSystemFolder(path, true);
139-
140-
return null;
141-
}
142-
143-
/// <summary>
144-
/// Gets a folder, given its path. Returns null if the folder does not exist.
145-
/// </summary>
146-
/// <param name="path">The path to a folder, as returned from the <see cref="IFolder.Path"/> property.</param>
147-
/// <param name="cancellationToken">The cancellation token.</param>
148-
/// <returns>A folder for the specified path, or null if it does not exist.</returns>
149-
public async Task<IFolder> GetFolderFromPathAsync(string path, CancellationToken cancellationToken)
150-
{
151-
Requires.NotNullOrEmpty(path, "path");
152-
153-
await AwaitExtensions.SwitchOffMainThreadAsync(cancellationToken);
154-
if (System.IO.Directory.Exists(path))
155-
return new FileSystemFolder(path, true);
95+
#elif ANDROID || __IOS__
96+
return LocalStorage;
97+
#endif
15698

157-
return null;
99+
return null;
100+
}
158101
}
159102
}
160103
}

0 commit comments

Comments
 (0)