/
FileStreamManager.cs
69 lines (59 loc) · 2.31 KB
/
FileStreamManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
namespace Samples.Sudoku
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
/// <summary>
/// Stream manager implementation for local files.
/// </summary>
public class FileStreamManager : IStreamManager
{
// Note the use of Lazy<T> here.
private Lazy<string> boardsDirectory = null;
/// <summary>
/// Initializes a new instance of the <see cref="FileStreamManager"/> class.
/// </summary>
/// <remarks>
/// Boards will be stored in the subdirectory "Boards" in the roaming application data
/// directory.
/// </remarks>
public FileStreamManager()
{
Contract.Ensures(this.boardsDirectory != null);
this.boardsDirectory = new Lazy<string>(FileStreamManager.GetBoardsDirectory);
}
/// <summary>
/// Initializes a new instance of the <see cref="FileStreamManager"/> class.
/// </summary>
/// <param name="boardsDirectory">Directory where the boards should be stored.</param>
public FileStreamManager(string boardsDirectory)
{
Contract.Ensures(this.boardsDirectory != null);
this.boardsDirectory = new Lazy<string>(() => boardsDirectory);
}
// Note the use of inheritdoc here. Documentation is inherited from interface.
/// <inheritdoc />
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Caller responsible for disposing")]
public Task<Stream> OpenStreamAsync(string boardName, AccessMode accessMode)
{
// Note that we do not need any preconditions here. They are inherited from
// the base interface.
return Task.FromResult(new FileStream(
Path.Combine(this.boardsDirectory.Value, boardName),
accessMode == AccessMode.Read ? FileMode.Open : FileMode.OpenOrCreate) as Stream);
}
private static string GetBoardsDirectory()
{
Contract.Ensures(Contract.Result<string>() != null);
Contract.Ensures(Contract.Result<string>().Length > 0);
var appDataPath = Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
// Note: Try this method in the debugger and check out the new features of the
// Autos window.
return Path.Combine(appDataPath, "Boards");
}
}
}