ProjectArcade
182 строки · 8.9 Кб
1using System;
2using System.Runtime.InteropServices;
3using System.Text;
4using DokanNet.Logging;
5using DokanNet.Native;
6
7namespace DokanNet
8{
9/// <summary>
10/// Helper methods to %Dokan.
11/// </summary>
12public class Dokan : IDisposable
13{
14private readonly ILogger _logger;
15private bool _disposed;
16
17/// <summary>
18/// Initialize all required Dokan internal resources.
19///
20/// This needs to be called only once before trying to use <see cref="Mount"/> or <see cref="CreateFileSystem"/> for the first time.
21/// Otherwise both will fail and raise an exception.
22/// </summary>
23/// <param name="logger"><see cref="ILogger"/> that will log all DokanNet debug informations.</param>
24public Dokan(ILogger logger)
25{
26_logger = logger;
27NativeMethods.DokanInit();
28}
29
30/// <summary>
31/// Unmount a dokan device from a driver letter.
32/// </summary>
33/// <param name="driveLetter">Driver letter to unmount.</param>
34/// <returns><c>true</c> if device was unmount
35/// -or- <c>false</c> in case of failure or device not found.</returns>
36public bool Unmount(char driveLetter)
37{
38return NativeMethods.DokanUnmount(driveLetter);
39}
40
41/// <summary>
42/// Unmount a dokan device from a mount point.
43/// </summary>
44/// <param name="mountPoint">Mount point to unmount (<c>Z</c>, <c>Z:</c>, <c>Z:\\</c>, <c>Z:\\MyMountPoint</c>).</param>
45/// <returns><c>true</c> if device was unmount
46/// -or- <c>false</c> in case of failure or device not found.</returns>
47public bool RemoveMountPoint(string mountPoint)
48{
49return NativeMethods.DokanRemoveMountPoint(mountPoint);
50}
51
52/// <summary>
53/// Retrieve native dokan dll version supported.
54/// </summary>
55/// <returns>Return native dokan dll version supported.</returns>
56public int Version { get { return (int)NativeMethods.DokanVersion(); }}
57
58/// <summary>
59/// Retrieve native dokan driver version supported.
60/// </summary>
61/// <returns>Return native dokan driver version supported.</returns>
62public int DriverVersion { get { return (int)NativeMethods.DokanDriverVersion(); } }
63
64/// <summary>
65/// Dokan User FS file-change notifications
66/// </summary>
67/// <remarks> If <see cref="DokanOptions.EnableNotificationAPI"/> is passed to <see cref="Dokan.Mount"/>,
68/// the application implementing the user file system can notify
69/// the Dokan kernel driver of external file- and directory-changes.
70///
71/// For example, the mirror application can notify the driver about
72/// changes made in the mirrored directory so that those changes will
73/// be automatically reflected in the implemented mirror file system.
74///
75/// This requires the filePath passed to the respective methods
76/// to include the absolute path of the changed file including the drive-letter
77/// and the path to the mount point, e.g. "C:\Dokan\ChangedFile.txt".
78///
79/// These functions SHOULD NOT be called from within the implemented
80/// file system and thus be independent of any Dokan file system operation.
81///</remarks>
82public class Notify
83{
84/// <summary>
85/// Notify Dokan that a file or directory has been created.
86/// </summary>
87/// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
88/// <param name="filePath">Absolute path to the file or directory, including the mount-point of the file system.</param>
89/// <param name="isDirectory">Indicates if the path is a directory.</param>
90/// <returns>true if the notification succeeded.</returns>
91public static bool Create(DokanInstance dokanInstance, string filePath, bool isDirectory)
92{
93return NativeMethods.DokanNotifyCreate(dokanInstance.DokanHandle, filePath, isDirectory);
94}
95
96/// <summary>
97/// Notify Dokan that a file or directory has been deleted.
98/// </summary>
99/// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
100/// <param name="filePath">Absolute path to the file or directory, including the mount-point of the file system.</param>
101/// <param name="isDirectory">Indicates if the path is a directory.</param>
102/// <returns>true if notification succeeded.</returns>
103/// <remarks><see cref="DokanOptions.EnableNotificationAPI"/> must be set in the mount options for this to succeed.</remarks>
104public static bool Delete(DokanInstance dokanInstance, string filePath, bool isDirectory)
105{
106return NativeMethods.DokanNotifyDelete(dokanInstance.DokanHandle, filePath, isDirectory);
107}
108
109/// <summary>
110/// Notify Dokan that file or directory attributes have changed.
111/// </summary>
112/// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
113/// <param name="filePath">Absolute path to the file or directory, including the mount-point of the file system.</param>
114/// <returns>true if notification succeeded.</returns>
115/// <remarks><see cref="DokanOptions.EnableNotificationAPI"/> must be set in the mount options for this to succeed.</remarks>
116public static bool Update(DokanInstance dokanInstance, string filePath)
117{
118return NativeMethods.DokanNotifyUpdate(dokanInstance.DokanHandle, filePath);
119}
120
121/// <summary>
122/// Notify Dokan that file or directory extended attributes have changed.
123/// </summary>
124/// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
125/// <param name="filePath">Absolute path to the file or directory, including the mount-point of the file system.</param>
126/// <returns>true if notification succeeded.</returns>
127/// <remarks><see cref="DokanOptions.EnableNotificationAPI"/> must be set in the mount options for this to succeed.</remarks>
128public static bool XAttrUpdate(DokanInstance dokanInstance, string filePath)
129{
130return NativeMethods.DokanNotifyXAttrUpdate(dokanInstance.DokanHandle, filePath);
131}
132
133/// <summary>
134/// Notify Dokan that a file or directory has been renamed.
135/// This method supports in-place rename for file/directory within the same parent.
136/// </summary>
137/// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
138/// <param name="oldPath">Old, absolute path to the file or directory, including the mount-point of the file system.</param>
139/// <param name="newPath">New, absolute path to the file or directory, including the mount-point of the file system.</param>
140/// <param name="isDirectory">Indicates if the path is a directory.</param>
141/// <param name="isInSameDirectory">Indicates if the file or directory have the same parent directory.</param>
142/// <returns>true if notification succeeded.</returns>
143/// <remarks><see cref="DokanOptions.EnableNotificationAPI"/> must be set in the mount options for this to succeed.</remarks>
144public static bool Rename(DokanInstance dokanInstance, string oldPath, string newPath, bool isDirectory, bool isInSameDirectory)
145{
146return NativeMethods.DokanNotifyRename(dokanInstance.DokanHandle, oldPath,
147newPath,
148isDirectory,
149isInSameDirectory);
150}
151}
152
153protected virtual void Dispose(bool disposing)
154{
155if (!_disposed)
156{
157if (disposing)
158{
159// dispose managed state (managed objects)
160}
161// free unmanaged resources
162NativeMethods.DokanShutdown();
163// set fields to null
164_disposed = true;
165}
166}
167
168public void Dispose()
169{
170// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
171Dispose(disposing: true);
172GC.SuppressFinalize(this);
173}
174
175// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
176~Dokan()
177{
178// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
179Dispose(disposing: false);
180}
181}
182}
183