从文件读取数据后如何删除文件 [英] How to delete file after reading data from file

查看:122
本文介绍了从文件读取数据后如何删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除c:\ TempDownloads \ TempFiles \
中的临时文件

how to delete temparary files in c:\TempDownloads\TempFiles\

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();
          
        }
      

    }

推荐答案

请参考以下内容:
http://stackoverflow.com/questions/400140/how-do- i-automatically-delete-tempfiles-in-c [ ^ ]
Please refer the following :
http://stackoverflow.com/questions/400140/how-do-i-automatically-delete-tempfiles-in-c[^]


这篇关于从文件读取数据后如何删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆