Exemplo n.º 1
0
        private bool IsFileLocked(string path)
        {
            System.IO.FileStream stream = null;

            try
            {
                stream = new System.IO.FileInfo(path).Open(System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
            }
            catch (System.IO.IOException)
            {
                return(true);
            }
            catch (UnauthorizedAccessException)
            {
                return(true);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
                ;
            }
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read information from device info file to instance of DeviceInfo
        /// </summary>
        /// <exception cref="HomeFileOrganizer.Exceptions.DeviceFileException"></exception>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Task readDeviceFile(string file, MyDevice device)
        {
            return(Task.Run(() =>
            {
                MyDisk disk = null;
                var hiddenrStream = new System.IO.FileInfo(Managers.FileManager.PathToHFOFolder + "\\DeviceInfos\\" + file).Open(System.IO.FileMode.Open);
                XmlReader reader = XmlTextReader.Create(hiddenrStream, new XmlReaderSettings()
                {
                    Async = true
                });
                try
                {
                    Stack <MyFolder> folderStack = new Stack <MyFolder>();
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            switch (reader.Name)
                            {
                            case "disk":
                                disk = device.disks.Find(i => i.Id == UInt64.Parse(reader.GetAttribute("id")));
                                if (disk == null)
                                {
                                    throw new Exceptions.DeviceFileException(String.Format("Not compatible overview file and device file {0}. Id {1} not found in overview file.", file, reader.GetAttribute("id")));
                                }
                                break;

                            case "file":
                                MyFile actFile;
                                if (folderStack.Count != 0)
                                {
                                    actFile = new MyFile(folderStack.Peek());
                                }
                                else
                                {
                                    actFile = new MyFile(new MyRootFolder("", disk, null));
                                }
                                String infoFileName = reader.GetAttribute("infoFile");
                                if (infoFileName != null)
                                {
                                    actFile.InfoFilePath = String.Format("\\FileInfo\\{0}\\File\\{1}", disk.Id, infoFileName);
                                }
                                actFile.Category = selectCategory(reader.GetAttribute("category"));
                                if (folderStack.Count == 0)
                                {
                                    actFile.FilePath = reader.GetAttribute("path");
                                    disk.files.Add(actFile);
                                }
                                else
                                {
                                    actFile.FilePath = folderStack.Peek().Path + reader.GetAttribute("path");
                                    folderStack.Peek().files.Add(actFile);
                                }
                                break;

                            case "folder":
                                MyFolder actFolder = null;
                                if (folderStack.Count == 0)
                                {
                                    actFolder = new MyRootFolder(reader.GetAttribute("path"), disk, selectCategory(reader.GetAttribute("category")));
                                    disk.folders.Add((MyRootFolder)actFolder);
                                }
                                else
                                {
                                    actFolder = new MyFolder(reader.GetAttribute("path"));
                                    actFolder.UpperFolder = folderStack.Peek();
                                    folderStack.Peek().folders.Add(actFolder);
                                }
                                String infoFilePath2 = reader.GetAttribute("infoFile");
                                if (infoFilePath2 != null)
                                {
                                    actFolder.InfoFilePath = String.Format("\\FileInfo\\{0}\\Folder\\{1}", disk.Id, infoFilePath2);
                                }
                                folderStack.Push(actFolder);
                                break;
                            }
                            break;

                        case XmlNodeType.EndElement:
                            switch (reader.Name)
                            {
                            case "folder":
                                folderStack.Pop();
                                break;
                            }
                            break;
                        }
                    }
                }
                finally
                {
                    reader.Dispose();
                    hiddenrStream.Dispose();
                }
            }));
        }