본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C#- XML 파일리스트 저장 ( ListView )

728x90
반응형


 WPF & C#- XML 파일리스트 저장 ( ListView  )



xmlWriteFile.zip






MainWidow.xaml


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<Window x:Class="xmlWriteFile6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:xmlWriteFile6"
        mc:Ignorable="d"
        Title="MainWindow" Height="535.5" Width="975.5">
    <Grid>
        <ListView x:Name="lvFile" HorizontalAlignment="Left" Height="439" Margin="10,10,0,0" VerticalAlignment="Top" Width="948" Drop="lvFile_Drop">
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="btnXmlWrite" Content="Xml Write" HorizontalAlignment="Left" Margin="10,454,0,0" VerticalAlignment="Top" Width="95" Height="41" Click="btnXmlWrite_Click"/>
 
    </Grid>
</Window>
cs




MainWidow.xaml.cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
 
namespace xmlWriteFile6
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            lvFile.AllowDrop = true;
        }
 
        private void lvFile_Drop(object sender, DragEventArgs e)
        {
            try
            {
                ListView ListView00 = sender as ListView;
 
                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
                    foreach (string str in file)
                    {
                        // 파일 드랍
                        if ((File.GetAttributes(str) & FileAttributes.Directory!= FileAttributes.Directory)
                        {
                            file_input(ListView00, str);  // 파일 정보 입력
                        }
                        else if ((File.GetAttributes(str) & FileAttributes.Directory== FileAttributes.Directory)
                        {
                            dirSearch(ListView00, str);  // 폴더 정보 입력
                        }
                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
 
        // 파일 정보 입력
        private void file_input(ListView ListView00, string str)
        {
            FileInfo fi = new FileInfo(str);
            if (fi.Exists)
            {
                double fileSize = fi.Length;
                var versionInfo = FileVersionInfo.GetVersionInfo(str);
                string fileVersion = versionInfo.ProductVersion;
                ListView00.Items.Add(str);
            }
        }
 
        // 폴더 정보 입력
        private void folder_input(ListView ListView00, string str)
        {
            ListView00.Items.Add(str);
        }
 
        // 재귀함수 - 폴더&파일
        private void dirSearch(ListView ListView00, string dir)
        {
            string[] Directories = Directory.GetDirectories(dir);   // Defalut Folder
            string[] Files = Directory.GetFiles(dir);   // File list Search
 
            foreach (string fileName in Files)   // File check
            {
                file_input(ListView00, fileName);   // add File
            }
 
            folder_input(ListView00, dir);   // add Folder
            foreach (string nodeDir in Directories)   // Folder list Search
            {
                dirSearch(ListView00, nodeDir);   // re dirSearch
            }
        }
 
        // 버튼 : XML 입력
        private void btnXmlWrite_Click(object sender, RoutedEventArgs e)
        {
            XDocument xdoc = new XDocument(new XDeclaration("1.0""UTF-16"null));
            XElement xroot = new XElement("FileInfo");
            xdoc.Add(xroot);
 
            foreach (string item in lvFile.Items)
            {
                FileInfo fileInfo = new FileInfo(item);
 
                //폴더정보입력
                if ((File.GetAttributes(item) & FileAttributes.Directory== FileAttributes.Directory)
                {
                    MessageBox.Show(item);
                }
 
                // 파일정보 입력
                if ((File.GetAttributes(item) & FileAttributes.Directory!= FileAttributes.Directory)
                {
                    string fileName = fileInfo.Name;
                    string fileFolder = fileInfo.Directory.FullName;
                    string baseFolder = Environment.CurrentDirectory;   // 기준폴더 위치
                    Uri relative_Path = new Uri(Environment.CurrentDirectory).MakeRelativeUri(new Uri(fileFolder));
                    fileFolder = relative_Path.ToString();
 
                    double fileSize = fileInfo.Length;
                    var versionInfo = FileVersionInfo.GetVersionInfo(item);
                    string LastWriteTime = File.GetLastWriteTime(item).ToString();  // 파일수정날짜 (업데이트 기초)
                    string fileVersion = versionInfo.ProductVersion;
 
                    XElement xe1 = new XElement("FileInfo",
                        new XAttribute("Name", fileName),
                        new XElement("Folder", fileFolder),
                        new XElement("Size", fileSize),
                        new XElement("LastWriteTime", LastWriteTime),
                        new XElement("Version", fileVersion)
                        );
                    xroot.Add(xe1);
                }
            }
            xdoc.Save(@".\update.xml");   // 파일 존재 시 자동 덮어쓰기 됨
        }
    }
 
}
cs




728x90
반응형