Exemplo n.º 1
0
 public WFile(FileInfo file, string fileType, WDirectory parentDirectory)
 {
     Name = file.Name;
     FileSystemItem = file;
     ParentDirectory = parentDirectory;
     FileType = fileType;
 }
 public NewFile(WDirectory directory)
 {
     InitializeComponent();
     Owner = Window.GetWindow(Application.Current.MainWindow);
     FileDirectory = directory;
     FileType = FileType = ProjectManagementConsts.GeneralScript;
 }
 public WDirectory(DirectoryInfo directory, WDirectory parentDirectory)
 {
     Name = directory.Name;
     FileSystemItem = directory;
     ParentDirectory = parentDirectory;
     Children = new BindingList<WPath>();
 }
        public void AddNewItem(WDirectory parentDir, string name, string fileType = null)
        {
            //check config for existing item
            if (parentDir.ContainsFile(name))
            {
                throw new Exception("Item already exists in project!");
            }

            var parentDirInfo = (DirectoryInfo) parentDir.FileSystemItem;
            var parentDirPath = parentDirInfo.FullName.Substring(
                parentDirInfo.FullName.IndexOf(ProjectPath.Split('\\').Last(),
                    StringComparison.Ordinal));

            //check filesystem for existing item
            if (fileType != null) // if file
            {
                var parentProjectDirectory = ParentDirInfo(parentDir);
                if (parentDirInfo.GetFiles().Any(item => name == item.Name))
                {
                    throw new Exception("Item already exists in directory!");
                }
                var element = new XElement(FileTag, new XAttribute(NameAttribute, name),
                    new XAttribute(TypeAttribute, fileType));
                //add file
                GetDirectoryElement(parentDirPath, Configuration.Root.Element(PathsTag).Element(DirectoryTag))
                    .Add(element);
                var filePath = $"{parentDirInfo.FullName}\\{name}";
                File.Create(filePath);
                parentProjectDirectory.Children.Add(new WFile
                {
                    FileSystemItem = new FileInfo(filePath),
                    FileType = fileType
                });

                SaveConfiguration();
            }
            else //if directory
            {
                if (parentDirInfo.GetDirectories().Any(item => name == item.Name))
                {
                    throw new Exception("This directory already exists!");
                }
                var element = new XElement(DirectoryTag, new XAttribute(NameAttribute, name));
                //add directory
                GetDirectoryElement(parentDirPath, Configuration.Root.Element(PathsTag).Element(DirectoryTag))
                    .Add(element);
                parentDir.Children.Add(new WDirectory
                {
                    FileSystemItem = Directory.CreateDirectory($"{parentDirInfo.FullName}\\{name}")
                });
                SaveConfiguration();
            }
        }
 public override void ReadXml(XmlReader reader)
 {
     if (reader.MoveToContent() != XmlNodeType.Element
         || reader.LocalName.ToLower() != "directory") return;
     Name = reader.GetAttribute("Name");
     var inner = reader.ReadSubtree();
     inner.MoveToContent();
     while (inner.Read())
     {
         if (inner.NodeType == XmlNodeType.Element)
         {
             if (inner.LocalName.ToLower() == "file")
             {
                 var file = new WFile();
                 file.ReadXml(inner);
                 Children.Add(file);
             }
             else if (inner.LocalName.ToLower() == "directory")
             {
                 var directory = new WDirectory();
                 directory.ReadXml(inner);
                 Children.Add(directory);
             }
         }
     }
     inner.Close();
 }