Fetch data from .cdr file using C#.net after fetching save in SQL database

i want to fetch data from .cdr file with c# code. and after that i want to save this data in my data base. so any one know how can i doing this.

  • Former Member
    Former Member over 7 years ago

    Without the use of com CorelDraw object, fetch text data from cdr

    I don't understand cdr file but this can be useful.

    I monitory a directory and open cdr files in my application

    My cdr file structure follow the print, you can open a cdr file in winrar or winzip to see better

    My Code, this code works but need improvements

      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
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.IO;
    using System.IO.Compression;
    using System.Data.SqlClient;
    using System.Xml;
    
    namespace OpenCDRFilesTest
    {
        public class DataItem
        {
            public string Name { get; set; }
            public string Email { get; set; }
            public string FileName { get; set; }
            public DataItem(){ }
            public DataItem(string name, string email)
            {
                this.Name = name;
                this.Email = email;
            }
        }
        public partial class Form1 : Form
        {
            private string directory = "C:\\Users\\Reginaldo\\Desktop\\watcher";
            private List<string> filePool = new List<string>();
            bool running = false;
            private string sqlConn = @"Server=REGINALDO-03\SQLEXPRESS;Database=CorelFiles;Trusted_Connection=True;";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                FileSystemWatcher fsw = new FileSystemWatcher(directory);
                fsw.NotifyFilter = NotifyFilters.LastWrite;
                fsw.Filter = "*.cdr";
                fsw.Changed += fsw_Changed;
                fsw.EnableRaisingEvents = true;
            }
            void processFile(string file)
            {
                DataItem Data = new DataItem();
                Data.FileName = file;
                string text;
                while (filePool.Count > 0)
                {
                    using (ZipArchive zipFile = new ZipArchive(File.Open(Path.Combine(directory,file), FileMode.Open)))
                    {
                        ZipArchiveEntry entry = zipFile.GetEntry("META-INF/textinfo.xml");
                        using (Stream stEntry = entry.Open())
                        {
                            using (StreamReader sr = new StreamReader(stEntry))
                            {
                                text = sr.ReadToEnd();
                            }
                        }
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml(text);
                        XmlNodeList elList = xmlDoc.GetElementsByTagName("rdf:Description")[0].ChildNodes;
                        XmlNodeList objList = null;
                        for (int i = 0; i < elList.Count; i++)
                        {
                            XmlNode Node = elList[i ];
                            string name = Node.Name;
                            if (name.Equals("ObjectNames"))
                            {
                                objList = Node.FirstChild.ChildNodes;
                                break;
                            }
                        }
                        for (int i = 0; i < elList.Count; i++)
                        {
                            XmlNode Node = elList[i ];
                            string name = Node.Name;
                            if (name.Equals("TextStream"))
                            {
                                for (int j = 0; j < objList.Count; j++)
                                {
                                    if (j == i)
                                    {
                                        if (objList[j].InnerText == "email")
                                            Data.Email = Node.InnerText;
                                        if (objList[j].InnerText == "name")
                                            Data.Name = Node.InnerText;
                                    }
                                }
                            }
                        }
                    }
                    filePool.RemoveAt(0);
                    InsertData(Data);
                    if (filePool.Count == 0)
                        this.running = false;
                }
            }
            private void InsertData(DataItem item)
            {
                SqlConnection conn = new SqlConnection(this.sqlConn);
                conn.Open();
                SqlCommand insertComand = new SqlCommand("INSERT INTO dbo.CorelFile (CorelFile_FileName,CorelFile_Name,CorelFile_Email) VALUES (@fileName,@name,@email)",conn);
                insertComand.Parameters.AddWithValue("@fileName", item.FileName);
                insertComand.Parameters.AddWithValue("@name", item.Name);
                insertComand.Parameters.AddWithValue("@email", item.Email);
                insertComand.ExecuteNonQuery();
                conn.Close();
            }
          
            void fsw_Changed(object sender, FileSystemEventArgs e)
            {
                filePool.Add(e.FullPath);
                if (!this.running)
                {
                    this.running = true;
                    processFile(e.Name);
                }
            }
        }
    }