Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

с# application settings MSDN


http://msdn.microsoft.com/ru-ru/library/wabtadw6.aspx пошаговое описание

http://msdn.microsoft.com/ru-ru/library/a65txexh.aspx сам мини кусок кода

Properties.Settings.Default.FirstUserSetting = "abc";
Properties.Settings.Default.Save();

c# универсальные шаблоны

// Declare the generic class.
public class GenericList<T>
{
    void Add(T input) { }
}
class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int.
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string.
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass.
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}

c# Delete and copy files with replacing

string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";

try
{
    string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
    string[] txtList = Directory.GetFiles(sourceDir, "*.txt");

    // Copy picture files.
    foreach (string f in picList)
    {
        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        // Use the Path.Combine method to safely append the file name to the path.
        // Will overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
    }

    // Copy text files.
    foreach (string f in txtList)
    {

        // Remove path from the file name.
        string fName = f.Substring(sourceDir.Length + 1);

        try
        {
            // Will not overwrite if the destination file already exists.
            File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
        }

        // Catch exception if the file was already copied.
        catch (IOException copyError)
        {
            Console.WriteLine(copyError.Message);
        }
    }

    // Delete source files that were copied.
    foreach (string f in txtList)
    {
        File.Delete(f);
    }
    foreach (string f in picList)
    {
        File.Delete(f);
    }
}

catch (DirectoryNotFoundException dirNotFound)
{
    Console.WriteLine(dirNotFound.Message);
}

c# How to work with ini files

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace sIniFile
{
    /// <summary>
    /// Descrizione di riepilogo per IniFileClass.
    /// </summary>
    public class IniFileClass
    {
        private String IniFile = "";
        
        // Importazione dei metodi dalle DLL di sistema
        [DllImport("kernel32")] private static extern long WritePrivateProfileInt(String Section, String Key, int Value, String FilePath);
        [DllImport("kernel32")] private static extern long WritePrivateProfileString(String Section, String Key, String Value, String FilePath);
        [DllImport("kernel32")] private static extern int GetPrivateProfileInt(String Section, String Key, int Default, String FilePath);
        [DllImport("kernel32")] private static extern int GetPrivateProfileString(String Section, String Key, String Default, StringBuilder retVal, int Size, String FilePath);

        public IniFileClass(String IniFile)
        {
            // Mi salvo il percorso del file INI
            this.IniFile = IniFile;
        }

        public String ReadString(String Section, String Key, String Default)
        {
            // Creo lo StringBuilder che conterra la stringa
            StringBuilder StrBu = new StringBuilder(255);
            // Provo a leggere il parametro richiesto
            GetPrivateProfileString(Section, Key, Default, StrBu, 255, IniFile);
            // Restituisco il parametro letto
            return StrBu.ToString();
        }

        public int ReadInt(String Section, String Key, int Default)
        {
            // Restituisco il valore del parametro letto
            return GetPrivateProfileInt(Section, Key, Default, IniFile); 
        }

        public void WriteString(String Section, String Key, String Value)
        {
            // Salvo il valore del parametro impostato
            WritePrivateProfileString(Section, Key, Value, IniFile);
        }

        public void WriteInt(String Section, String Key, int Value)
        {
            // Salvo il valore del parametro impostato
            WritePrivateProfileInt(Section, Key, Value, IniFile);
        }
    }
}

textblock wrap text font

Отличный мануал про это и всё другое http://msdn.microsoft.com/en-us/library/cc189010(v=vs.95).aspx

c# Как отправить емэйл ( email ) / c# How to send email

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("tomail@mail.ru");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("frommail@mail.ru");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient _SmtpClient = new System.Net.Mail.SmtpClient("smtp.mail.ru");
_SmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
System.Net.NetworkCredential _NetworkCredential = new System.Net.NetworkCredential("smtpuser", "smtppassword");
_SmtpClient.UseDefaultCredentials = false;
_SmtpClient.Credentials = _NetworkCredential;

System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:\\textfile.txt");
    message.Attachments.Add(attachment);
_SmtpClient.Send(message);


вот здесь можно ещё почитать
http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Send-Email-Using-SMTP-Server.html

C# Write and read config file

app.config

  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <add key="oldPlace" value="4" />  
  5.   </appSettings>  
  6. </configuration>  

Write:


  1. System.Configuration.Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
  2. config.AppSettings.Settings["oldPlace"].Value = "3";       
  3. config.Save(ConfigurationSaveMode.Modified);  
  4. ConfigurationManager.RefreshSection("appSettings");  

Read:

  1. string value = ConfigurationManager.AppSettings["oldPlace"];