Beispiel #1
0
 public int WriteFile(string filename, byte[] buffer, ref uint writtenBytes, long offset, DokanFileInfo info)
 {
     try
     {
         var path = this.GetPath(filename);
         if (!File.Exists(path))
         {
             return(DokanNet.ERROR_FILE_NOT_FOUND);
         }
         File.WriteAllBytes(path, buffer);
         var file = MemoryFileManager.GetInstance().GetFile(path);
         if (file != null && file.FileStatus != FileStatusEnum.Create && file.FileStatus != FileStatusEnum.Remove)
         {
             var fileInfo   = new FileInfo(path);
             var memoryFile = new MemoryFile
             {
                 CreateDate     = fileInfo.CreationTime,
                 FilePath       = path.Replace(this.root_, ""),
                 FileStatus     = FileStatusEnum.Append,
                 FileType       = FileTypeEnum.File,
                 LastModifyDate = fileInfo.LastWriteTime
             };
             MemoryFileManager.GetInstance().SetFile(memoryFile);
         }
         return(DokanNet.DOKAN_SUCCESS);
     }
     catch (Exception)
     {
         return(DokanNet.DOKAN_SUCCESS);
     }
 }
Beispiel #2
0
        public int DeleteDirectory(string filename, DokanFileInfo info)
        {
            try
            {
                if (filename == "\\")
                {
                    return(DokanNet.DOKAN_SUCCESS);
                }
                var path = this.GetPath(filename);
                if (Directory.Exists(path))
                {
                    var dir        = new DirectoryInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = dir.CreationTime,
                        FilePath       = path.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.Directory,
                        LastModifyDate = dir.LastWriteTime
                    };

                    Directory.Delete(path, true);

                    MemoryFileManager.GetInstance().SetFile(memoryFile);
                }

                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Beispiel #3
0
        public int DeleteFile(string filename, DokanFileInfo info)
        {
            try
            {
                var path = this.GetPath(filename);
                if (File.Exists(path))
                {
                    var fileInfo   = new FileInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = fileInfo.CreationTime,
                        FilePath       = path.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileInfo.LastWriteTime
                    };

                    File.Delete(path);

                    MemoryFileManager.GetInstance().SetFile(memoryFile);
                }
                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Beispiel #4
0
 /// <summary>
 /// 添加或修改文件信息到缓存中
 /// </summary>
 /// <param name="memoryFile"></param>
 public void SetFile(MemoryFile memoryFile)
 {
     lock (_fileLock)
     {
         var file = memoryFiles.FirstOrDefault(p => p.FilePath == memoryFile.FilePath);
         if (file == null)
         {
             memoryFiles.Add(memoryFile);
         }
         else
         {
             file.FilePath       = memoryFile.FilePath;
             file.FileStatus     = memoryFile.FileStatus;
             file.FileType       = memoryFile.FileType;
             file.CreateDate     = memoryFile.CreateDate;
             file.LastModifyDate = memoryFile.LastModifyDate;
             if (file.FileType == FileTypeEnum.Directory && file.FileStatus == FileStatusEnum.Remove)
             {
                 var files = memoryFiles.Where(p => p.FilePath.Contains(memoryFile.FilePath)).ToList();
                 if (files.Count > 0)
                 {
                     foreach (var f in files)
                     {
                         f.FileStatus = FileStatusEnum.Remove;
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
        /// <summary>
        /// 获取某个文件的缓存项信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public MemoryFile GetFile(string filePath)
        {
            lock (_fileLock)
            {
                MemoryFile file = memoryFiles.FirstOrDefault(p => p.FilePath == filePath);

                return(file);
            }
        }
Beispiel #6
0
        /// <summary>
        /// 初始化文件缓存信息。
        /// </summary>
        public static void GetLocalFileData()
        {
            try
            {
                var root      = LocalDiskPathHelper.GetPath();
                var rootFiles = (new DirectoryInfo(root)).GetFiles();
                if (rootFiles.Length > 0)
                {
                    foreach (var rootFile in rootFiles)
                    {
                        var memoryFile = new MemoryFile();
                        memoryFile.CreateDate     = rootFile.CreationTime;
                        memoryFile.FilePath       = rootFile.FullName.Replace(root, "");
                        memoryFile.FileStatus     = FileStatusEnum.Normal;
                        memoryFile.FileType       = FileTypeEnum.File;
                        memoryFile.LastModifyDate = rootFile.LastWriteTime;
                        GetInstance().SetFile(memoryFile);
                    }
                }

                var dirs = new List <DirectoryInfo>();

                GetALlDirectoryInfo(root, ref dirs);

                if (dirs.Count > 0)
                {
                    foreach (var dir in dirs)
                    {
                        var memoryFile = new MemoryFile();
                        memoryFile.CreateDate     = dir.CreationTime;
                        memoryFile.FilePath       = dir.FullName.Replace(root, "");
                        memoryFile.FileStatus     = FileStatusEnum.Normal;
                        memoryFile.FileType       = FileTypeEnum.Directory;
                        memoryFile.LastModifyDate = dir.LastWriteTime;
                        GetInstance().SetFile(memoryFile);

                        var files = dir.GetFiles();

                        foreach (var file in files)
                        {
                            var memoryFile1 = new MemoryFile();
                            memoryFile1.CreateDate     = file.CreationTime;
                            memoryFile1.FilePath       = file.FullName.Replace(root, "");
                            memoryFile1.FileStatus     = FileStatusEnum.Normal;
                            memoryFile1.FileType       = FileTypeEnum.File;
                            memoryFile1.LastModifyDate = file.LastWriteTime;
                            GetInstance().SetFile(memoryFile1);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Beispiel #7
0
        public int MoveFile(string filename, string newname, bool replace, DokanFileInfo info)
        {
            try
            {
                if (filename == "\\")
                {
                    return(DokanNet.DOKAN_SUCCESS);
                }
                var newPath = this.GetPath(newname);
                var path    = this.GetPath(filename);

                if (string.IsNullOrEmpty(Path.GetExtension(newPath)))
                {
                    var dir = new DirectoryInfo(path);

                    var oldFiles =
                        MemoryFileManager.GetInstance().GetAllFiles().Where(p => p.FilePath.Contains(filename)).ToList();

                    Directory.Move(path, newPath);

                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = dir.CreationTime,
                        FilePath       = dir.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.Directory,
                        LastModifyDate = dir.LastWriteTime
                    };

                    MemoryFileManager.GetInstance().SetFile(memoryFile);

                    foreach (var oldFile in oldFiles)
                    {
                        var memoryFileNew = new MemoryFile()
                        {
                            CreateDate     = oldFile.CreateDate,
                            FilePath       = oldFile.FilePath.Replace(filename, newname),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = oldFile.FileType,
                            LastModifyDate = oldFile.LastModifyDate
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFileNew);
                    }
                }
                else
                {
                    var fileOld    = new FileInfo(path);
                    var memoryFile = new MemoryFile
                    {
                        CreateDate     = fileOld.CreationTime,
                        FilePath       = fileOld.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Remove,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileOld.LastWriteTime
                    };

                    File.Move(path, newPath);

                    var fileNew = new FileInfo(newPath);

                    var memoryFileNew = new MemoryFile
                    {
                        CreateDate     = fileNew.CreationTime,
                        FilePath       = fileNew.FullName.Replace(this.root_, ""),
                        FileStatus     = FileStatusEnum.Create,
                        FileType       = FileTypeEnum.File,
                        LastModifyDate = fileNew.LastWriteTime
                    };

                    MemoryFileManager.GetInstance().SetFile(memoryFile);

                    MemoryFileManager.GetInstance().SetFile(memoryFileNew);
                }

                return(DokanNet.DOKAN_SUCCESS);
            }
            catch (Exception)
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }
Beispiel #8
0
        public int CreateFile(string filename, FileAccess access,
                              FileShare share, FileMode mode, FileOptions options, DokanFileInfo info)
        {
            try
            {
                string path = GetPath(filename);
                if (Directory.Exists(path))
                {
                    info.IsDirectory = true;
                    return(DokanNet.DOKAN_SUCCESS);
                }
                switch (mode)
                {
                case FileMode.Append:
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.Create:
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();

                        var fileInfo   = new FileInfo(path);
                        var memoryFile = new MemoryFile
                        {
                            CreateDate     = fileInfo.CreationTime,
                            FilePath       = path.Replace(this.root_, ""),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = FileTypeEnum.File,
                            LastModifyDate = fileInfo.LastWriteTime
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFile);
                    }
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.CreateNew:
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();

                        var fileInfo   = new FileInfo(path);
                        var memoryFile = new MemoryFile
                        {
                            CreateDate     = fileInfo.CreationTime,
                            FilePath       = path.Replace(this.root_, ""),
                            FileStatus     = FileStatusEnum.Create,
                            FileType       = FileTypeEnum.File,
                            LastModifyDate = fileInfo.LastWriteTime
                        };
                        MemoryFileManager.GetInstance().SetFile(memoryFile);
                    }
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.Open:
                    return(DokanNet.DOKAN_SUCCESS);

                case FileMode.OpenOrCreate:
                    return(DokanNet.DOKAN_SUCCESS);
                }
                return(-DokanNet.ERROR_FILE_NOT_FOUND);
            }
            catch
            {
                return(DokanNet.DOKAN_SUCCESS);
            }
        }