· 8 years ago · Aug 25, 2018, 11:50 AM
1Stream that inherits class Stream and can be serialized C#
2public static byte[] ReadToEnd(this Stream s) {
3 using(var ms = new MemoryStream()) {
4 s.CopyTo(ms);
5 return ms.ToArray();
6 }
7}
8
9CREATE TABLE [dbo].[Files]
10(
11 [ID] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
12 [Name] NVARCHAR(255) NOT NULL,
13 [Storage] UNIQUEIDENTIFIER NOT NULL
14);
15
16/// <summary>
17/// Writes a stream to a file and returns a <see cref="Guid"/> that
18/// can be used to retrieve it again.
19/// </summary>
20/// <param name="incomingFile">The incoming file.</param>
21/// <returns>The <see cref="Guid"/> that should be used to identify the file.</returns>
22public static Guid WriteFile(Stream incomingFile)
23{
24 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApplication");
25 path = Path.Combine(path, "BinaryData");
26
27 var guid = Guid.NewGuid();
28 var ba = guid.ToByteArray();
29
30 // Create the path for the GUID.
31 path = Path.Combine(ba[0].ToString("x2"));
32 path = Path.Combine(ba[1].ToString("x2"));
33 path = Path.Combine(ba[2].ToString("x2"));
34 Directory.CreateDirectory(path); // Always succeeds, even if the directory already exists.
35
36 path = Path.Combine(guid.ToString() + ".dat");
37 using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
38 {
39 var buffer = new byte[Environment.SystemPageSize];
40 var length = 0;
41 while ((length = incomingFile.Read(buffer, 0, buffer.Length)) != 0)
42 fs.Write(buffer, 0, buffer.Length);
43 }
44
45 return guid;
46}
47
48/// <summary>
49/// Deletes a file created by <see cref="WriteFile"/>.
50/// </summary>
51/// <param name="guid">The original <see cref="Guid"/> that was returned by <see cref="WriteFile"/>.</param>
52public static void DeleteFile(Guid guid)
53{
54 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApplication");
55 path = Path.Combine(path, "BinaryData");
56
57 var ba = guid.ToByteArray();
58
59 // Create the path for the GUID.
60 path = Path.Combine(ba[0].ToString("x2"));
61 path = Path.Combine(ba[1].ToString("x2"));
62 path = Path.Combine(ba[2].ToString("x2"));
63 path = Path.Combine(guid.ToString() + ".dat");
64 if (File.Exists(path))
65 File.Delete(path);
66}
67
68/// <summary>
69/// Reads the a file that was created by <see cref="WriteFile"/>.
70/// </summary>
71/// <param name="guid">The original <see cref="Guid"/> that was returned by <see cref="WriteFile"/>.</param>
72/// <returns>The stream that can be used to read the file.</returns>
73public static Stream ReadFile(Guid guid)
74{
75 var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyApplication");
76 path = Path.Combine(path, "BinaryData");
77
78 var ba = guid.ToByteArray();
79
80 // Create the path for the GUID.
81 path = Path.Combine(ba[0].ToString("x2"));
82 path = Path.Combine(ba[1].ToString("x2"));
83 path = Path.Combine(ba[2].ToString("x2"));
84 path = Path.Combine(guid.ToString() + ".dat");
85 return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
86}