Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to delete temparary files in c:\TempDownloads\TempFiles\

C#
public partial class Attach_Download : Form
    {
        public Attach_Download()
        {
            InitializeComponent();
        }

      
        
        private void btnDownload_Click(object sender, EventArgs e)
        {
                        
        }


        

        private void Attach_Download_Load(object sender, EventArgs e)
        {
            try
            {
                lbldate.Text=System.DateTime.Now.ToLongDateString();
                Directory.Delete(@"C:\TempDownloads", true);
            }
            catch (Exception err)
            {
            }
            using (Imap imap = new Imap())
            {
                imap.Connect("smtp.ascent-group.com");
                imap.Login("software@ascent-group.com", "ascent44");

                imap.SelectInbox();
                List<long> uids = imap.SearchFlag(Flag.All);
                if (uids.Count < 1)
                {
                    txtNoFax.Text = "MailBox is empty";
                }
                else
                {
                    txtNoFax.Text = uids.Count.ToString();
                }
               
            }
        }
        public void createPatientList()
        {
            string dirStr2 = "c:/PatientList2";

            if (Directory.Exists(dirStr2))
            {

            }
            else
            {
                Directory.CreateDirectory(dirStr2);

            }
            string str1 = "c:/TempDownloads/ImageFiles";
            if (Directory.Exists(str1))
            {

            }
            else
            {
                Directory.CreateDirectory(str1);

            }

            string str2 = "c:/TempDownloads/TempFiles";
            if (Directory.Exists(str2))
            {

            }
            else
            {
                Directory.CreateDirectory(str2);

            }
        }

        public static string Extracttext(string Url)
        {
            if (Url.Contains(@"Patient Name: "))
            {
                Url = Url.Split(new string[] { "Patient Name: " }, 2, StringSplitOptions.None)[1];
            }
            else if (Url.Contains(@"Pt. Name: "))
            {
                Url = Url.Split(new string[] { "Pt. Name: " }, 2, StringSplitOptions.None)[1];
            }
            else if (Url.Contains(@"PATIENT NAME: "))
            {
                Url = Url.Split(new string[] { "PATIENT NAME: " }, 2, StringSplitOptions.None)[1];
            }
            else if (Url.Contains(@"PATIENT: "))
            {
                Url = Url.Split(new string[] { "PATIENT: " }, 2, StringSplitOptions.None)[1];
            }
            else
            {
                Url = "";
            }
            return Url.Split('\r')[0];
        }
       
       
        private void btnsave_Click(object sender, EventArgs e)
        {
            lblmessage.Text = "Please wait while Downloading Faxes";
            //Please purchase Mail.dll license at http://www.lesnikowski.com/mail/
            string dirStr = "c:/Downloads2";


            string currentdt = String.Format("{0:dd-MM-yyyy}", System.DateTime.Now);
            string str = "c:/Downloads2/" + currentdt.ToString();
            string dirStr2 = "c:/TempDownloads/TempFiles";

            if (Directory.Exists(dirStr))
            {


            }
            else
            {

                Directory.CreateDirectory(dirStr);

            }

            if (Directory.Exists(str))
            {

            }
            else
            {
                Directory.CreateDirectory(str);
            }

            if (Directory.Exists(str))
            {

                using (Imap imap = new Imap())
                {
                    imap.Connect("smtp.ascent-group.com");
                    imap.Login("software@ascent-group.com", "ascent44");

                    imap.SelectInbox();
                    List<long> uids = imap.SearchFlag(Flag.All);
                    if (uids.Count < 1)
                    {
                        MessageBox.Show("MailBox is empty");
                    }
                    else
                    {

                        foreach (long uid in uids)
                        {


                            string eml = imap.GetMessageByUID(uid);
                            IMail email = new MailBuilder()
                                .CreateFromEml(eml);

                            // save all attachments to disk
                            email.Attachments.ForEach(mime => mime.Save(str + "/" + mime.SafeFileName));
                            createPatientList();
                            email.Attachments.ForEach(mime => mime.Save(dirStr2 + "/" + +uid + ".pdf"));

                            //break;
                           
                            //Convert pdf to .tiff


                            string path = @"c:\TempDownloads\TempFiles\" + uid + ".pdf";//+ txtPatientNm.Text + "-" + String.Format("{0:MM-dd-yyyy}", dateTimePicker1.Text.ToString()) + ".pdf";
                            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
                            {
                                document = new Document(file);
                            }


                            ConvertToTiffOptions tiffOptions = new ConvertToTiffOptions(getDPI(), getCompression());

                            string tiffile = @"c:\TempDownloads\ImageFiles\" + uid + ".tiff";//Image Files\" + txtPatientNm.Text + "-" + String.Format("{0:MM-dd-yyyy}", dateTimePicker1.Text.ToString()) + ".tiff";
                            using (FileStream file = new FileStream(tiffile, FileMode.Create, FileAccess.Write))
                            {
                                document.ConvertToTiff(file, tiffOptions);
                                file.Close();
                                
                            }

                            //OCR(get Patient Name)
                            try
                            {
                                CheckFileType(@"c:\TempDownloads\ImageFiles\");
                               
                                //*************************************
                                //***************************************
                                string ln = "";
                                // File.Delete(@"C:\PatientList2\ImageFiles\temp.tiff");

                                string Message = txtPatientNm.Text.ToString();
                                string[] Record = Regex.Split(Message.Trim(), " ");
                                ln = Record[1].ToString();
                                string fn = Record[0].ToString();

                                string strPList = "c:/PatientList2/";
                                //email.Attachments.ForEach(mime => mime.Save(strPList + "/" + ln + "-" + fn + "-" + String.Format("{0:MM-dd-yyyy}", dateTimePicker1.Text.ToString()) + ".pdf"));
                                if (ln != "" && fn != "")
                                {
                                    email.Attachments.ForEach(mime => mime.Save(strPList + "/" + ln + "-" + fn + ".pdf"));
                                }
                                else
                                {
                                    email.Attachments.ForEach(mime => mime.Save(strPList + "/" + mime.SafeFileName));
                                }
                                
                            }
                            catch (Exception err)
                            {
                                string strPList = "c:/PatientList2/";
                                email.Attachments.ForEach(mime => mime.Save(strPList + "/" + mime.SafeFileName));
                            }
                                                    
                           
                            //Delete mail message
                            //imap.DeleteMessageByUID(uid);
                        }

                        imap.Close();
                        MessageBox.Show("Attachment downloaded successfully....");
                        lblmessage.Text = "";
                        btnsave.Enabled = false;


                    }
                }

            }

          
           
        }


        int getDPI()
        {
           
            return 96;
            throw new Exception("Unsupported DPI selected.");
        }

        TiffCompression getCompression()
        {
          
            return TiffCompression.None;
            throw new Exception("Unsupported Compression selected.");
        }

        Document document = null;

        public void CheckFileType(string directoryPath)
        {
            IEnumerator files = Directory.GetFiles(directoryPath).GetEnumerator();
            while (files.MoveNext())
            {
              
                //get file extension 
                string fileExtension = Path.GetExtension(Convert.ToString(files.Current));

                //get file name without extenstion 
                string fileName = Convert.ToString(files.Current).Replace(fileExtension, string.Empty);

                //Check for JPG File Format 
                if (fileExtension == ".tiff" || fileExtension == ".TIFF") // or // ImageFormat.Jpeg.ToString()
                {
                    try
                    {
                        //OCR Operations ... 

                        MODI.Document md = new MODI.Document();
                        md.Create(Convert.ToString(files.Current));
                        md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
                        string myText = "";
                        for (int i = 0; i < md.Images.Count; i++)
                        {
                            MODI.Image image = (MODI.Image)md.Images[i];
                          
                            myText = myText + image.Layout.Text.ToString();
                           
                        }
                                               
                        
                        //********************
                        

                      txtPatientNm.Text = Extracttext(myText).ToString();
                      
                                          
                    }
                    catch (Exception)
                    {
                        //MessageBox.Show("This Image hasn't a text or has a problem", 
                        //"OCR Notifications", 
                        //MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    }
                }
              
            }
        }
       

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
          
        }
      

    }
Posted
Updated 29-Sep-11 22:09pm
v3
Comments
RaisKazi 30-Sep-11 2:55am    
Enclose your code block in "<pre></pre>" Tags.
Timberbird 30-Sep-11 3:06am    
Could you provide more info? For example, when do you want to delete these files? Are you going to delete all files or just files of specific type? Do you want to delete inner directories, if any?
BTW, this code screams for method like "EnsureDirectoryExists(string sPath)" :)
anjali2 30-Sep-11 3:35am    
having extension .tiff from ImageFiles folder in my case
BobJanova 30-Sep-11 5:25am    
You delete files with File.Delete. I'm guessing your title isn't actually the question you're trying to ask ...

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900