terça-feira, 28 de julho de 2015

C# Basic Operations - WinForms

           This code is useful for WinForms:


  • ZIP and Sent by SMTP


   using (ZipFile zip = new ZipFile())
            {
                zip.AddFile(jsonFolder + "/files/teste001.txt", ".");
                zip.Save(jsonFolder + "/files/final.zip");
            }

            MailMessage mensagemEmail = new MailMessage();
            mensagemEmail.From = new MailAddress("lapuinka@gmail", "lapuinka@gmail");
            foreach (string email in emails)
            {
                mensagemEmail.To.Add(email);
            }
            mensagemEmail.Subject = "YOUR SUBJECT";
            mensagemEmail.Body = "YOUR MESSAGE BODY. .";

            mensagemEmail.Attachments.Add(new Attachment(jsonFolder + @"\files\final.zip"));

            SmtpClient client = new SmtpClient("smtp.gmail", 25);
            client.Credentials = new NetworkCredential("lapuinka@gmail", "yourpassword");

            // envia a mensagem

            client.Send(mensagemEmail);

  • Dialog Open

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "CSV|*.csv";
            DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtSelectedFile.Text = ofd.FileName;

            }



      private void btSelectFile_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtSelectedFolder.Text = fbd.SelectedPath;
            }

        }
           

  •  Write a File


          FileStream fs = new FileStream(OutputDir + "/sample.json", FileMode.Create, FileAccess.Write);
                        UTF8Encoding enc = new UTF8Encoding();
                        byte[] fileBuffer = enc.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(bwj, new Newtonsoft.Json.Converters.IsoDateTimeConverter()));
                        fs.Write(fileBuffer, 0, fileBuffer.Length);
                        fs.Close();


  •  Datetime Operation


DateTime dt1 = Convert.ToDateTime(data2, CultureInfo.GetCultureInfo("pt-BR"));
                       
                        //2 anos atras
                        DateTime twoYears = DateTime.Now;
                        twoYears = twoYears.AddYears(-2);

                        if (dt1.CompareTo(twoYears) <= 0)
                        {
                            dt1 = new DateTime(2012, 8, 25);
                            
                        }


  •  Process a File




    public ProcessInit()
        {
            InitializeComponent();

            StreamReader sr = new StreamReader("init.ini");
           
            importerNumber = Convert.ToInt32(sr.ReadLine());
            batchSize = Convert.ToInt32(sr.ReadLine());
            emails = new List<string>();
            string email = sr.ReadLine();
            while (email != null)
            {
                emails.Add(email);
                email = sr.ReadLine();
            }

            sr.Close();
        }

  •  Get All File in a Directory


     FileInfo[] files = di.GetFiles();
           
            int errorQuantity = 0;

            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Extension != ".json")
                {
                    continue;
                }
                StreamReader sr = files[i].OpenText();
                string json = sr.ReadToEnd();
                sr.Close();

                Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
.
.
.


Postar um comentário