lunes, 20 de abril de 2015

Folder Watcher C#

using System;
using System.IO;
using System.Security.Permissions;

public class Watcher
{

    public static void Main()
    {
        while(true)
            Run();
    }

    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void Run()
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = "C:\\Files"; //args[1];
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }

    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
}



source: msdn.com

lunes, 6 de abril de 2015

Extending TextBox component with integrated text




private void tbName_Enter(object sender, EventArgs e)
{
   lbName.Visible = false;
   tbName.BackColor = System.Drawing.Color.FromArgb(34, 34, 34); ;
}

private void tbName_Leave(object sender, EventArgs e)

{
   lbName.Visible = tbName.Text.Trim().Equals(string.Empty);
   tbName.BackColor = System.Drawing.Color.FromArgb(64, 64, 64); 
}

private void lbName_Click(object sender, EventArgs e)
{
   tbName.Focus();
}


UI CODE

// 
// lbName
// 
this.lbName.BackColor = System.Drawing.Color.FromArgb(64, 64, 64);
this.lbName.CausesValidation = false;
this.lbName.ForeColor = System.Drawing.Color.Silver;
this.lbName.Location = new System.Drawing.Point(130, 136);
this.lbName.Name = "lbName";
this.lbName.Size = new System.Drawing.Size(479, 34);
this.lbName.TabIndex = 1;
this.lbName.Text = "Data Context Available for Edition";
this.lbName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
// 
// tbName
// 
this.tbName.BackColor = System.Drawing.Color.FromArgb(64, 64, 64);
this.tbName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.tbName.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tbName.ForeColor = System.Drawing.Color.Silver;
this.tbName.Location = new System.Drawing.Point(128, 134);
this.tbName.Name = "tbName";
this.tbName.Size = new System.Drawing.Size(498, 38);
this.tbName.TabIndex = 2;
this.tbName.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.tbName.Enter += new System.EventHandler(this.textBox2_Enter);
this.tbName.Leave += new System.EventHandler(this.tbName_Leave);