Monday, December 21, 2009

How to Get the date-time of last windows shutdown using .NET

The solution is below, DateTime can be achieved  from a byte array with fewer statements using the BitConverter.The following  code will give the correct DateTime from the registry

 public static DateTime GetLastShutdownDate()

{
    string rKey = @"System\CurrentControlSet\Control\Windows";
    Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(rKey);

    string rValueName = "ShutdownTime";
    byte[] val = (byte[]) key.GetValue(rValueName);
    long AsLongValue= BitConverter.ToInt64(val, 0);
    return DateTime.FromFileTime(AsLongValue);
}

Saturday, December 19, 2009

How to save Gtalk conversion into the database in C# ?

In my recent project i work on a module where we need to track the Gtalk conversion into our sql server database and whenever required we have to show the report. basically you can say that its an application for tracking our employee !!!!!!!
Here is the code from where we are connected to the Gtalk ...
This is a sample of my module in future i will submit the whole code here ..

private void btnStart_Click(object sender, EventArgs e)
{
User = txtUsername.Text;
Pwd = txtPassword.Text;
jabberClient1.User = User;
jabberClient1.Server = "gmail.com";
jabberClient1.Password = Pwd;
jabberClient1.AutoRoster = true;
rm = new RosterManager();
rm.Stream = jabberClient1;
rm.AutoSubscribe = true;
rm.AutoAllow = jabber.client.AutoSubscriptionHanding.AllowAll;
rm.OnRosterBegin += new bedrock.ObjectHandler(rm_OnRosterBegin);
rm.OnRosterEnd += new bedrock.ObjectHandler(rm_OnRosterEnd);
rm.OnRosterItem += new RosterItemHandler(rm_OnRosterItem);
rosterCount = 1;
pm = new PresenceManager();
pm.Stream = jabberClient1;
jabberClient1.Connect();
jabberClient1.OnAuthenticate += new bedrock.ObjectHandler(jabberClient1_OnAuthenticate);

}

Wednesday, December 16, 2009

Ajax BubbleTooltip

I got 40 tool tips from this site and these tool tips are really good
Here is the link from where you can see the demo and download the code

http://www.smashingmagazine.com/2007/06/12/tooltips-scripts-ajax-javascript-css-dhtml/

Tuesday, December 15, 2009

How to take ScreenShot in C#

Here by using this code i try to take the Screen Shot of my desktop and it works same as the print screen button does. Here the parameter filename is use for saving the screen shot image in a specified location.

private static Bitmap bmpScreenshot;

private static Graphics gfxScreenshot;

private void TakeScreenShot(string filename)

        {

            this.Hide();

            bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);

            gfxScreenshot = Graphics.FromImage(bmpScreenshot);

            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

            bmpScreenshot.Save(filename, ImageFormat.Png);

            attachedFile = filename;

            this.Show();

            SendMail(filename);

        }

How to give full access to a folder in C#

How to Give Permissions to a folder ? Here is the Solution
public static void SetPermissions(string dirPath)
{
DirectoryInfo info = new DirectoryInfo(dirPath);
WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent();
DirectorySecurity ds = info.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule(self.Name,
FileSystemRights.FullControl,
InheritanceFlags.ObjectInherit |
InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow));
info.SetAccessControl(ds);
}