Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
highlevel:csharp:snippets [2009/07/05 18:23] alfred |
highlevel:csharp:snippets [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 30: | Línea 30: | ||
| System.Console.WriteLine("Difference in days: {0} ", differenceInDays); | System.Console.WriteLine("Difference in days: {0} ", differenceInDays); | ||
| </code> | </code> | ||
| + | |||
| Línea 54: | Línea 55: | ||
| <code csharp> | <code csharp> | ||
| StreamWriter sw = new StreamWriter(File.Open(sFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); | StreamWriter sw = new StreamWriter(File.Open(sFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); | ||
| + | </code> | ||
| + | |||
| + | === Escribir en UTF8 === | ||
| + | <code csharp> | ||
| + | System.IO.TextWriter tw = new System.IO.StreamWriter( | ||
| + | new System.IO.FileStream(path + "\\index.html", System.IO.FileMode.Create), | ||
| + | System.Text.Encoding.UTF8); | ||
| + | tw.Write(body); | ||
| + | tw.Close(); | ||
| </code> | </code> | ||
| Línea 76: | Línea 86: | ||
| lang1 = node.ChildNodes[0].InnerText; | lang1 = node.ChildNodes[0].InnerText; | ||
| } | } | ||
| + | </code> | ||
| + | |||
| + | ==== Expresiones regulares ==== | ||
| + | === Eliminar las tags de un texto === | ||
| + | <code csharp> | ||
| + | using System.Text.RegularExpressions; | ||
| + | public string Strip(string text) | ||
| + | { | ||
| + | return Regex.Replace(text, @”<(.|\n)*?>”, string.Empty); | ||
| + | } | ||
| + | </code> | ||
| + | ==== Descargar archivos de internet ==== | ||
| + | <code csharp> | ||
| + | System.Net.WebClient wc = new System.Net.WebClient(); | ||
| + | wc.DownloadFile("http://url/imagen.jpg", "c:\\img.jpg"); | ||
| </code> | </code> | ||
| Línea 259: | Línea 284: | ||
| } | } | ||
| </code> | </code> | ||
| + | |||
| + | |||
| Línea 358: | Línea 385: | ||
| return buffer; | return buffer; | ||
| </code> | </code> | ||
| + | |||
| + | |||
| Línea 432: | Línea 461: | ||
| WindowsIdentity id = WindowsIdentity.GetCurrent(); | WindowsIdentity id = WindowsIdentity.GetCurrent(); | ||
| id.name; | id.name; | ||
| + | </code> | ||
| + | === Lanzar un comando y quedarte con su salida === | ||
| + | <code csharp> | ||
| + | public static string Command(string command, string pars) | ||
| + | { | ||
| + | System.Diagnostics.Process process = new System.Diagnostics.Process(); | ||
| + | process.StartInfo.FileName = command; | ||
| + | process.StartInfo.Arguments = pars; | ||
| + | |||
| + | try | ||
| + | { | ||
| + | process.StartInfo.CreateNoWindow = true; | ||
| + | process.StartInfo.UseShellExecute = false; | ||
| + | process.StartInfo.RedirectStandardOutput = true; | ||
| + | process.StartInfo.WorkingDirectory = Environment.CurrentDirectory; | ||
| + | |||
| + | process.Start(); | ||
| + | process.WaitForExit(Int32.MaxValue); | ||
| + | } | ||
| + | catch (Exception exc) | ||
| + | { | ||
| + | return exc.Message + "\n" + exc.StackTrace; | ||
| + | } | ||
| + | |||
| + | string ConsoleOutput = process.StandardOutput.ReadToEnd(); | ||
| + | return ConsoleOutput; | ||
| + | } | ||
| + | </code> | ||
| + | === Encriptar en MD5 === | ||
| + | <code csharp> | ||
| + | using System; | ||
| + | using System.Text; | ||
| + | using System.Security.Cryptography; | ||
| + | ... | ||
| + | public string EncodePassword(string originalPassword) | ||
| + | { | ||
| + | Byte[] originalBytes; | ||
| + | Byte[] encodedBytes; | ||
| + | MD5 md5 = new MD5CryptoServiceProvider(); | ||
| + | originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword); | ||
| + | encodedBytes = md5.ComputeHash(originalBytes); | ||
| + | return BitConverter.ToString(encodedBytes); | ||
| + | } | ||
| + | </code> | ||
| + | ... o ... | ||
| + | <code csharp> | ||
| + | System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider(); | ||
| + | byte[] bs = System.Text.Encoding.UTF8.GetBytes(text); | ||
| + | bs = x.ComputeHash(bs); | ||
| + | System.Text.StringBuilder s = new System.Text.StringBuilder(); | ||
| + | foreach (byte b in bs) | ||
| + | s.Append(b.ToString("x2").ToLower()); | ||
| + | return s.ToString(); | ||
| + | </code> | ||
| + | |||
| + | === Aplicación en background === | ||
| + | <code csharp> | ||
| + | class Server { | ||
| + | public void Start() { | ||
| + | lock (this) { | ||
| + | // Hacer algo | ||
| + | Monitor.Wait(this); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public void Stop() | ||
| + | { | ||
| + | lock (this) | ||
| + | { | ||
| + | // Hacer algo | ||
| + | Monitor.PulseAll(this); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | static void Main(string[] args) { | ||
| + | new Server().Start(); | ||
| + | } | ||
| + | } | ||
| </code> | </code> | ||
| Línea 450: | Línea 557: | ||
| conn.Close(); | conn.Close(); | ||
| </code> | </code> | ||
| + | |||
| Línea 463: | Línea 571: | ||
| con.Close(); | con.Close(); | ||
| </code> | </code> | ||
| + | |||