ProjectArcade

Форк
0
182 строки · 8.9 Кб
1
using System;
2
using System.Runtime.InteropServices;
3
using System.Text;
4
using DokanNet.Logging;
5
using DokanNet.Native;
6

7
namespace DokanNet
8
{
9
    /// <summary>
10
    /// Helper methods to %Dokan.
11
    /// </summary>
12
    public class Dokan : IDisposable
13
    {
14
        private readonly ILogger _logger;
15
        private 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>
24
        public Dokan(ILogger logger)
25
        {
26
            _logger = logger;
27
            NativeMethods.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>
36
        public bool Unmount(char driveLetter)
37
        {
38
            return 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>
47
        public bool RemoveMountPoint(string mountPoint)
48
        {
49
            return 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>
56
        public 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>
62
        public 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>
82
        public 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>
91
            public static bool Create(DokanInstance dokanInstance, string filePath, bool isDirectory)
92
            {
93
                return 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>
104
            public static bool Delete(DokanInstance dokanInstance, string filePath, bool isDirectory)
105
            {
106
                return 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>
116
            public static bool Update(DokanInstance dokanInstance, string filePath)
117
            {
118
                return 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>
128
            public static bool XAttrUpdate(DokanInstance dokanInstance, string filePath)
129
            {
130
                return 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>
144
            public static bool Rename(DokanInstance dokanInstance, string oldPath, string newPath, bool isDirectory, bool isInSameDirectory)
145
            {
146
                return NativeMethods.DokanNotifyRename(dokanInstance.DokanHandle, oldPath,
147
                    newPath,
148
                    isDirectory,
149
                    isInSameDirectory);
150
            }
151
        }
152

153
        protected virtual void Dispose(bool disposing)
154
        {
155
            if (!_disposed)
156
            {
157
                if (disposing)
158
                {
159
                    // dispose managed state (managed objects)
160
                }
161
                //  free unmanaged resources
162
                NativeMethods.DokanShutdown();
163
                // set fields to null
164
                _disposed = true;
165
            }
166
        }
167

168
        public void Dispose()
169
        {
170
            // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
171
            Dispose(disposing: true);
172
            GC.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
179
            Dispose(disposing: false);
180
        }
181
    }
182
}
183

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.