tag:blogger.com,1999:blog-74246282903109136182024-03-13T10:52:15.478-07:00Technical BlogKomal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-7424628290310913618.post-23880326256455428042016-11-15T22:22:00.001-08:002016-11-15T22:22:26.414-08:00How to host WCF service in IIS manager<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; font-family: inherit;">To host the WCF service, follow the below steps:</span><div>
<ol>
<li><span style="background-color: white; font-family: inherit;">Add virtual directory in "IIS Manager"</span><ol>
<li><span style="background-color: white; font-family: inherit;">PC Name -> Sites -> Default Web Site -> Add virtual directory</span></li>
<li><span style="background-color: white; font-family: inherit;">Type in the alias (name) and browse to the physical path (where .svc file resides).</span></li>
</ol>
</li>
<li><span style="background-color: white; font-family: inherit;">Right click on the added application, select "Convert to application".</span></li>
<li><span style="background-color: white; font-family: inherit;">Test the hosted application by clicking the "Browse button".</span></li>
</ol>
<div>
<span style="background-color: white; font-family: inherit;"><br /></span></div>
<div>
<span style="background-color: white; font-family: inherit;"><b>Possible issues:</b></span></div>
<ol>
<li><span style="background-color: white; color: red; font-family: inherit;">HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.</span></li>
</ol>
<div>
<span style="background-color: white; font-family: inherit;"><b>Solution:</b></span></div>
<div>
<span style="background-color: white; font-family: inherit;">Turn on/off windows features: Make sure that the below features are turned on:</span></div>
<div>
<ul style="text-align: left;">
<li><span style="background-color: white; font-family: inherit;">.net framework 3.5 and 4.5.</span></li>
<li><span style="background-color: white; font-family: inherit;">IIS</span></li>
</ul>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwtbEtUbOiSsXlCBp-Q-xv5T8r0zxWnk7TqrYtFJ_Ai08N5cM7UOIqpglMtePkk6zv_gyAiw8Xxk1QPF5mdBupYmhaevITYcRvXNmT0lAf5_07R9AfQcyRFdnk449slTRd12T4io3YkCot/s1600/Features.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwtbEtUbOiSsXlCBp-Q-xv5T8r0zxWnk7TqrYtFJ_Ai08N5cM7UOIqpglMtePkk6zv_gyAiw8Xxk1QPF5mdBupYmhaevITYcRvXNmT0lAf5_07R9AfQcyRFdnk449slTRd12T4io3YkCot/s320/Features.png" width="320" /></a></div>
<div>
<br /></div>
</div>
<br />
<ol start="2">
<li><div style="border: 0px; margin: 0px 0px 0.5em; padding: 0px;">
<span style="background-color: white; color: red; font-family: inherit;">If you can't access WCF service hosted on IIS remotely but can access it locally</span></div>
</li>
</ol>
<span style="background-color: white; font-family: inherit;"><b>Solution: </b></span><br />Make sure firewall on the hosted machine is OFF.</div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-66098098453563764132016-09-28T02:23:00.000-07:002016-09-28T02:23:06.654-07:00How to Enable, Disable and Start the task of task scheduler in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
Below function demonstrates how to enable, disable and run the already created scheduled task. It will first check if the task exists in task scheduler:<br />
<br />
public static void updateUserTaskInScheduler(string action)<br />
{<br />
try<br />
{<br />
ProcessStartInfo startInfo = new ProcessStartInfo();<br />
startInfo.FileName = "cmd.exe";<br />
startInfo.Arguments = "/C schtasks /query /TN <<TaskNameWithQuotes>>; //Check if task exists<br />
startInfo.RedirectStandardOutput = true;<br />
startInfo.UseShellExecute = false;<br />
startInfo.CreateNoWindow = true;<br />
startInfo.WindowStyle = ProcessWindowStyle.Hidden;<br />
if (System.Environment.OSVersion.Version.Major < 6)<br />
{<br />
startInfo.Verb = "runas";<br />
}<br />
using (Process process = Process.Start(startInfo))<br />
{<br />
// Read in all the text from the process with the StreamReader.<br />
using (StreamReader reader = process.StandardOutput)<br />
{<br />
string stdout = reader.ReadToEnd();<br />
if (stdout.Contains("<<TaskName>>")) //If task exists<br />
{<br />
startInfo.RedirectStandardOutput = false;<br />
startInfo.UseShellExecute = true;<br />
switch (action)<br />
{<br />
case "Enable":<br />
startInfo.Arguments = "/C schtasks /Change /TN <<TaskNameWithQuotes>> /Enable";<br />
break;<br />
<br />
case "Disable":<br />
startInfo.Arguments = "/C schtasks /Change /TN <<TaskNameWithQuotes>> /Disable";<br />
break;<br />
<br />
case "Run":<br />
startInfo.Arguments = "/C schtasks /RUN /TN <<TaskNameWithQuotes>> ";<br />
break;<br />
}<br />
Process.Start(startInfo).WaitForExit();<br />
}<br />
else<br />
{<br />
//Task doesnot exist<br />
}<br />
stdout = null;<br />
reader.Close();<br />
reader.Dispose();<br />
}<br />
}<br />
startInfo = null;<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
<br />
The above function can be called in the below manner:<br />
<br />
1. To run the task:<br />
updateUserTaskInScheduler("Run");<br />
<br />
2. To enable the task:<br />
updateUserTaskInScheduler("Enable");<br />
<br />
3. To disable the task:<br />
updateUserTaskInScheduler("Disable");</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com1tag:blogger.com,1999:blog-7424628290310913618.post-19982731850166630612016-08-26T06:19:00.000-07:002016-08-26T06:19:02.179-07:00How to load registry hive of logged off users in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
In HKEY_USERS, SIDs of only logged in users are present by default. If you need to make changes in any user's registry in logged off state, you will have to load the registry hive of that particular user.<br />
<br />
Registry hive can be loaded by using NTUser.dat file loacted in "C:\Users\{Username}". {Username} is the name of the logged off user.<br />
<br />
Following is the sample code required for this task:<br />
<br />
internal static readonly IntPtr HKEY_USERS = new IntPtr(unchecked((int)0x80000003));<br />
<br />
[DllImport("advapi32.dll", SetLastError = true)]<br />
static extern Int32 RegLoadKey(IntPtr hKey, string lpSubKey, string lpFile);<br />
<br />
Create a below function:<br />
<br />
public static void loadUserRegistryHive(string username, string keyName)<br />
{<br />
try<br />
{<br />
string path ="C:\Users\{Username}\NTUSER.DAT";<br />
FileInfo fi = new FileInfo(path);<br />
if (fi.Exists)<br />
{<br />
int retVal = RegLoadKey(HKEY_USERS, keyName, path);<br />
if (retVal == 0)<br />
MessageBox.Show("Success");<br />
else<br />
MessageBox.Show("Failure"); <br />
}<br />
else<br />
{<br />
MessageBox.Show("NTUSER.DAT file does not exist in path");<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
General Exception:<br />
<br />
While using RegLoadKey in the above function, you may get 1314 as the return value. This is the indication that you don't have the necessary privileges to load the registry hive. To do so, use the below code:<br />
<br />
public static void adjustPriviledge()<br />
{<br />
try<br />
{<br />
// Access token handle reused within the using block.<br />
using (AccessTokenHandle accessTokenHandle =<br />
Process.GetCurrentProcess().GetAccessTokenHandle(<br />
TokenAccessRights.AdjustPrivileges | TokenAccessRights.Query))<br />
{<br />
// Enable privileges using the same access token handle.<br />
AdjustPrivilegeResult backupResult = accessTokenHandle.EnablePrivilege(Privilege.Backup);<br />
AdjustPrivilegeResult restoreResult = accessTokenHandle.EnablePrivilege(Privilege.Restore);<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
The above function will add the SE_BACKUP_NAME and SE_RESTORE_NAME privileges required to load the registry hive.<br />
<br />
To use the function above you will have to include the below dll:<br />
ProcessPrivileges.dll<br />
<br />
You can download it from here:<br />
https://processprivileges.codeplex.com/<br />
<br />
It's an open source dll:<br />
License details: https://processprivileges.codeplex.com/license<br />
<br />
You will have to call function "adjustPriviledge" in function "loadUserRegistryHive" just before calling function "RegLoadKey" as shown below:<br />
<br />
adjustPriviledge();<br />
int retVal = RegLoadKey(HKEY_USERS, keyName, path);<br />
<br /></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com1tag:blogger.com,1999:blog-7424628290310913618.post-45452119212893218962016-05-04T00:22:00.001-07:002016-05-04T00:23:27.756-07:00How to clear clipboard text using C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
Known exceptions:<br />
<br />
Case 1:<br />
It you use<b> Clipboard.SetText(string.Empty)</b> to clear clipboard text, you will get an <i>ArgumentNullException</i> exception: "Value cannot be NULL".<br />
<br />
It is because you cannot set empty string or null using <b>Clipboard.SetText </b>function.<br />
<br />
Case 2:<br />
If you use <b>Clipboard.Clear()</b> function, you will get the below error:<br />
<br />
<i>"An unhandled exception of type 'System.Threading.ThreadStateException' occurred in System.Windows.Forms.dll</i><br />
<i><br /></i>
<i>Additional information: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it."</i><br />
<i><br /></i>
To fix the above 2 exceptions, use the following code:<br />
<br />
<b> public void deleteClipboardText()</b><br />
{<br />
try<br />
{<br />
Thread th = new Thread(new ThreadStart(clearClipboardText));<br />
th.SetApartmentState(ApartmentState.STA);<br />
th.Start();<br />
th.Join();<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(ex.Message);<br />
}<br />
}<br />
<br />
public void clearClipboardText()<br />
{<br />
System.Windows.Forms.Clipboard.Clear();<br />
}</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-40345167250522757752016-04-29T03:06:00.001-07:002016-04-29T03:06:43.178-07:00How to get Clipboard data and its size in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
Following function will get the clipboard data:<br />
<br />
[DllImport("user32.dll")]<br />
static extern IntPtr GetClipboardData(uint uFormat);<br />
[DllImport("user32.dll")]<br />
static extern bool IsClipboardFormatAvailable(uint format);<br />
[DllImport("user32.dll", SetLastError = true)]<br />
static extern bool OpenClipboard(IntPtr hWndNewOwner);<br />
[DllImport("user32.dll", SetLastError = true)]<br />
static extern bool CloseClipboard();<br />
[DllImport("kernel32.dll")]<br />
static extern IntPtr GlobalLock(IntPtr hMem);<br />
[DllImport("kernel32.dll")]<br />
static extern bool GlobalUnlock(IntPtr hMem);<br />
<br />
const uint CF_UNICODETEXT = 13;<br />
public static string GetText()<br />
{<br />
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))<br />
return null;<br />
if (!OpenClipboard(IntPtr.Zero))<br />
return null;<br />
<br />
string data = null;<br />
var hGlobal = GetClipboardData(CF_UNICODETEXT);<br />
if (hGlobal != IntPtr.Zero)<br />
{<br />
var lpwcstr = GlobalLock(hGlobal);<br />
if (lpwcstr != IntPtr.Zero)<br />
{<br />
data = Marshal.PtrToStringUni(lpwcstr);<br />
GlobalUnlock(lpwcstr);<br />
}<br />
}<br />
CloseClipboard();<br />
<br />
return data;<br />
}<br />
<br />
<br />
In order to get the size of clipboard text, you would need to copy the text in a file and then get the size of file as shown in the below code:<br />
<br />
public long getClipboardTextSize()<br />
{<br />
string text = GetText();<br />
System.IO.File.WriteAllText("C:\\ClipboardText.txt", text);<br />
FileInfo fi = new FileInfo(path);<br />
if (fi.Exists)<br />
{<br />
return fi.Length;<br />
}<br />
else<br />
{<br />
return 0;<br />
}<br />
}<br />
}</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-55525103262448717022016-04-28T02:20:00.001-07:002016-04-28T02:20:51.790-07:00Get startup programs of all users using C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
The below code will demonstrate how to list all the startup programs (also listed in msconfig) using C#.Net.<br />
<br />
A custom class (cStartupPrograms) is added to store a startup program and it's relevant properties.<br />
<br />
public class cStartupPrograms<br />
{<br />
public int StartupProgramId { get; set; }<br />
public int ComputerId { get; set; }<br />
public int StartupClassificationId { get; set; }<br />
public string StartupClassificationName { get; set; }<br />
public string ProgramName { get; set; }<br />
public string ProgramPath { get; set; }<br />
public bool? IsEnabled { get; set; }<br />
public bool? IsDeleted { get; set; }<br />
public string StartupType { get; set; }<br />
public string Publisher { get; set; }<br />
public string startupUserName { get; set; }<br />
}<br />
<br />
Below are the locations from where the startup programs are retrieved:<br />
<br />
public static List<string> startupProgramsRegistryNames_Enabled = new List<string>()<br />
{<br />
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", //HKLM, HKCU<br />
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run", //HKLM<br />
@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"<br />
};<br />
<br />
public static List<string> startupProgramsRegistryNames_Disabled = new List<string>()<br />
{<br />
@"SOFTWARE\Microsoft\Shared Tools\MSConfig\startupfolder", //HKLM<br />
@"SOFTWARE\Microsoft\Shared Tools\MSConfig\startupreg" //HKLM <br />
};<br />
<br />
public static List<string> startupProgramsRegistryNames_Enabled_OtherUsers = new List<string>()<br />
{<br />
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", //HKEY_USERS<br />
@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"<br />
};<br />
<br />
public static string startupProgramPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup";<br />
<br />
<br />
Below function will retrieve the list of startup program of all users:<br />
private void getAllStartupPrograms()<br />
{<br />
List<cStartupPrograms> cStartupProgramList = null;<br />
<br />
RegistryInfo rInfo1 = new RegistryInfo();<br />
rInfo1.registryKey = Registry.CurrentUser;<br />
<br />
RegistryInfo rInfo2 = new RegistryInfo();<br />
rInfo2.registryKey = Registry.LocalMachine;<br />
<br />
List<RegistryInfo> rInfoList = new List<RegistryInfo>();<br />
rInfoList.Add(rInfo1);<br />
rInfoList.Add(rInfo2);<br />
<br />
try<br />
{<br />
cStartupProgramList = new List<cStartupPrograms>();<br />
//Enabled programs<br />
//All users<br />
string[] files = Directory.GetFiles(startupProgramPath, "*");<br />
foreach (string fileName in files)<br />
{<br />
if (File.Exists(fileName))<br />
{<br />
string target = GetShortcutTargetFile(fileName);<br />
if (!String.IsNullOrEmpty(target))<br />
{<br />
cStartupPrograms startupProgram = new cStartupPrograms();<br />
<br />
startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);<br />
startupProgram.IsDeleted = false;<br />
startupProgram.IsEnabled = true;<br />
startupProgram.ProgramName = System.IO.Path.GetFileName(fileName);<br />
startupProgram.ProgramPath = target;<br />
startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();<br />
startupProgram.startupUserName = "All Users";<br />
startupProgram.StartupType =startupProgramPath;<br />
<br />
//if (!cStartupProgramList.Exists(p => p.ProgramName == fileName && p.ProgramPath == target))<br />
cStartupProgramList.Add(startupProgram);<br />
}<br />
}<br />
}<br />
<br />
//All users<br />
foreach (RegistryInfo rInfo in rInfoList)<br />
{<br />
RegistryKey regKey = rInfo.registryKey;<br />
foreach (string registryName in startupProgramsRegistryNames_Enabled)<br />
{<br />
string runOrRunOnceString = null;<br />
int n = registryName.LastIndexOf("\\");<br />
runOrRunOnceString = registryName.Substring(n + 1);<br />
using (RegistryKey startupKey = regKey.OpenSubKey(registryName))<br />
{<br />
if (startupKey == null)<br />
continue;<br />
<br />
var valueNames = startupKey.GetValueNames();<br />
foreach (var name in valueNames)<br />
{<br />
cStartupPrograms startupProgram = new .cStartupPrograms();<br />
<br />
startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);<br />
startupProgram.IsDeleted = false;<br />
startupProgram.IsEnabled = true;<br />
startupProgram.ProgramName = name;<br />
startupProgram.ProgramPath = Convert.ToString(startupKey.GetValue(name));<br />
startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();<br />
startupProgram.startupUserName = "All Users";<br />
startupProgram.StartupType = startupKey.ToString();<br />
<br />
//if (!cStartupProgramList.Exists(p => p.ProgramName == name && p.ProgramPath == startupProgram.ProgramPath))<br />
cStartupProgramList.Add(startupProgram);<br />
}<br />
}<br />
}<br />
}<br />
<br />
//Other users<br />
string[] hkeyUserNames = Registry.Users.GetSubKeyNames();<br />
foreach (string key in hkeyUserNames)<br />
{<br />
string userName = GetOwnerName(key);<br />
if (string.IsNullOrEmpty(userName))<br />
continue;<br />
<br />
RegistryKey startupKey = Registry.Users.OpenSubKey(key);<br />
if (startupKey == null)<br />
continue;<br />
<br />
foreach (string registryName in startupProgramsRegistryNames_Enabled_OtherUsers)<br />
{<br />
string runOrRunOnceString = null;<br />
int n = registryName.LastIndexOf("\\");<br />
runOrRunOnceString = registryName.Substring(n + 1);<br />
<br />
using (RegistryKey userKey = startupKey.OpenSubKey(registryName))<br />
{<br />
if (userKey == null)<br />
continue;<br />
<br />
var valueNames = userKey.GetValueNames();<br />
foreach (var name in valueNames)<br />
{<br />
cStartupPrograms startupProgram = new cStartupPrograms();<br />
startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);<br />
startupProgram.IsDeleted = false;<br />
startupProgram.IsEnabled = true;<br />
startupProgram.ProgramName = name;<br />
startupProgram.ProgramPath = Convert.ToString(userKey.GetValue(name));<br />
startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();<br />
startupProgram.startupUserName = userName;<br />
startupProgram.StartupType = userKey.ToString();<br />
//if (!cStartupProgramList.Exists(p => p.ProgramName == name && p.ProgramPath == startupProgram.ProgramPath && p.startupUserName==userName))<br />
cStartupProgramList.Add(startupProgram);<br />
}<br />
}<br />
}<br />
}<br />
<br />
//Disabled programs<br />
foreach (string registryName in startupProgramsRegistryNames_Disabled)<br />
{<br />
using (RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) //<br />
{<br />
using (RegistryKey startupkey = hklm.OpenSubKey(registryName))<br />
{<br />
<br />
if (startupkey == null)<br />
continue;<br />
<br />
foreach (string key in startupkey.GetSubKeyNames())<br />
{<br />
using (RegistryKey rk = startupkey.OpenSubKey(key))<br />
{<br />
cStartupPrograms startupProgram = new cStartupPrograms();<br />
<br />
startupProgram.ComputerId = Convert.ToInt32(frmPreferences.ComputerId);<br />
startupProgram.IsDeleted = false;<br />
startupProgram.IsEnabled = false;<br />
startupProgram.ProgramName = Convert.ToString(rk.GetValue("item"));<br />
startupProgram.ProgramPath = Convert.ToString(rk.GetValue("command"));<br />
startupProgram.StartupClassificationName = cEnum.eStartupClassification.Windows.ToString();<br />
var hkeyVal = rk.GetValue("hkey");<br />
if (hkeyVal != null)<br />
{<br />
string keyVal = Convert.ToString(rk.GetValue("key"));<br />
string runOrRunOnceString = null;<br />
int n = keyVal.LastIndexOf("\\");<br />
runOrRunOnceString = keyVal.Substring(n + 1);<br />
<br />
if (hkeyVal.ToString() == "HKLM")<br />
{<br />
startupProgram.StartupType = Registry.LocalMachine.ToString() + "\\" + keyVal;<br />
<br />
}<br />
else if (hkeyVal.ToString() == "HKCU")<br />
{<br />
startupProgram.StartupType = Registry.CurrentUser.ToString() + "\\" + keyVal;<br />
<br />
}<br />
}<br />
else<br />
{<br />
startupProgram.StartupType = Convert.ToString(rk.GetValue("location"));<br />
}<br />
startupProgram.startupUserName = "All Users";<br />
<br />
//if (!cStartupProgramList.Exists(p => p.ProgramName == startupProgram.ProgramName && p.ProgramPath == startupProgram.ProgramPath))<br />
cStartupProgramList.Add(startupProgram);<br />
<br />
}<br />
}<br />
}<br />
}<br />
}<br />
}<br />
catch (Exception ex)<br />
{<br />
<br />
return;<br />
}<br />
}</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-59333364234652639502016-04-13T00:18:00.001-07:002016-04-13T00:18:43.398-07:00Convert content of XML file to JSON string in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
The below function will convert the content of XML file to JSON string:<br />
<br />
private string convertXMLToJSON()<br />
{<br />
string xmlLogPath = "<<Path to the xml file>>";<br />
<br />
// Load the XML file<br />
var xDocument = System.Xml.Linq.XDocument.Load(xmlLogPath);<br />
<br />
// Read the XML file content and write it to string<br />
string xml = xDocument.ToString();<br />
<br />
// Load the XML string<br />
XmlDocument doc = new XmlDocument();<br />
doc.LoadXml(xml);<br />
<br />
// Convert the XML string to JSON string<br />
string jsonText = JsonConvert.SerializeXmlNode(doc);<br />
<br />
//Release the memory<br />
doc = null;<br />
xmlLogPath = null;<br />
xml = null;<br />
xDocument = null;<br />
<br />
//Return the JSON string<br />
return jsonText;<br />
<br />
}<br />
<br />
<u>IMPORTANT NOTE:</u><br />
<br />
Releasing object memory is very important. Even if your object went out of scope, it is not removed from the garbage collection. It happens because the variable still has some value in it. So Garbage collector think that the variable is in use and it doesn't remove it from the memory.<br />
<br />
Setting it to <b>NULL</b>, tells the garbage collector that the object is no longer in use and can be removed from the garbage collection.</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-31685913080903183202016-04-12T00:19:00.000-07:002016-04-12T00:21:57.008-07:00Start and stop a console application using C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
Start a console application:</h3>
<div>
<br /></div>
<div>
<div>
System.Diagnostics.Process aspProcess = null;</div>
<div>
private void start_console_app()</div>
<div>
{</div>
<div>
try</div>
<div>
{</div>
<div>
string pathToExe = @"<<Path to the exe >> ";</div>
<div>
</div>
<div>
if (aspProcess == null)</div>
<div>
{</div>
<div>
aspProcess = new System.Diagnostics.Process();</div>
<div>
}</div>
<div>
</div>
<div>
//Path and file name of command to run</div>
<div>
aspProcess.StartInfo.FileName = pathToExe;</div>
<div>
aspProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;</div>
<div>
<br /></div>
<div>
//Parameters to pass to program</div>
<div>
aspProcess.StartInfo.Arguments = "<<arguments you want to pass>>";</div>
<div>
<br /></div>
<div>
aspProcess.StartInfo.UseShellExecute = true;</div>
<div>
aspProcess.StartInfo.CreateNoWindow = true;</div>
<div>
<br /></div>
<div>
//Start the process</div>
<div>
aspProcess.Start();</div>
<div>
<br /></div>
<div>
//Wait for process to finish</div>
<div>
aspProcess.WaitForExit();</div>
<div>
aspProcess = null;</div>
<div>
}</div>
<div>
catch (Exception ex)</div>
<div>
{</div>
<div>
MessageBox.Show(ex.Message);</div>
<div>
}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<div>
1. For starting a console application, you would need to create an object of class "System.Diagnostics.Process".</div>
<div>
2. "FileName" is the path of the exe (console application).</div>
<div>
3. "Arguments" - It will send arguments to your console application.</div>
<div>
4. ProcessWindowStyle.Hidden will hide the console window.</div>
<div>
5. Start () method will start the process.<br />
<br /></div>
<h3 style="text-align: left;">
Stop a console application by sending "Control + C" Signal:</h3>
<div>
<br /></div>
<div>
<div>
public void stop_console_app()</div>
<div>
{</div>
<div>
// Release the current console, as you cannot attach 2 consoles at the same time</div>
<div>
if (aspProcess != null)</div>
<div>
{</div>
<div>
uint pid = (uint)aspProcess.Id;</div>
<div>
FreeConsole();</div>
<div>
<br /></div>
<div>
// This does not require the console window to be visible.</div>
<div>
if (AttachConsole(pid))</div>
<div>
{</div>
<div>
// Disable Ctrl-C handling for our program</div>
<div>
SetConsoleCtrlHandler(null, true);</div>
<div>
GenerateConsoleCtrlEvent(CtrlTypes.CTRL_C_EVENT, 0);</div>
<div>
<br /></div>
<div>
// Must wait here. If we don't and re-enable Ctrl-C</div>
<div>
// handling below too fast, we might terminate ourselves.</div>
<div>
Thread.Sleep(2000);</div>
<div>
<br /></div>
<div>
FreeConsole();</div>
<div>
<br /></div>
<div>
// Re-enable Ctrl-C handling or any subsequently started</div>
<div>
// programs will inherit the disabled state.</div>
<div>
SetConsoleCtrlHandler(null, false);</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<div>
You can add "Console_CancelKeyPress" event in your console application to nicely terminate it. If you want to call this method from another application (from where you've started your console application), add the above method.</div>
<div>
<br /></div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-8509975554438556322015-11-26T01:17:00.000-08:002015-11-27T01:06:53.297-08:00How to make the Windows Desktop application work well on High-DPI Displays and Fix Blurry Fonts<div dir="ltr" style="text-align: left;" trbidi="on">
If you change the DPI of your machine from 100% to any other higher value, your desktop applications may appear somewhat blurry when you compare them with other applications on the screen. Alternately, display problems may exist such as truncated text.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0fi54wolCPvPCRmoFPCclzANecSRqmnVWZEQ1Zfp9Wr2_ujHSq0HBykYw4xp225O-BtfiKFnCDgfB6KjbHEb4I6Bty-QcUYUfz6wB2y3PLZqV0CAZjHnleyAHDaAOl9qAknm3IJkdAYTs/s1600/display.png"><img border="0" height="305" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0fi54wolCPvPCRmoFPCclzANecSRqmnVWZEQ1Zfp9Wr2_ujHSq0HBykYw4xp225O-BtfiKFnCDgfB6KjbHEb4I6Bty-QcUYUfz6wB2y3PLZqV0CAZjHnleyAHDaAOl9qAknm3IJkdAYTs/s400/display.png" width="400" /></a><br />
<br />
In order to fix this issue, you would need to Disable DPI virtualization for the application. To do this, right-click the application’s shortcut and then click <b>Properties</b>. On the <b>Compatibility </b>tab, select <b>Disable Display Scaling on High DPI Settings</b>, and then click OK. <br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHsDvFpJLXhqCzgtLEdcQ9qqqmk5F__-HL9s2wzxFENitjlSLzIJard4IyXsY1n7R3jawmsz3hEWWuwsH_vw2xlvefxvRZIZUUbIP_zNmUuDUlVNkBtVeum4tBsSkn6C7hu0dRQtP9AhUK/s1600/property.png"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHsDvFpJLXhqCzgtLEdcQ9qqqmk5F__-HL9s2wzxFENitjlSLzIJard4IyXsY1n7R3jawmsz3hEWWuwsH_vw2xlvefxvRZIZUUbIP_zNmUuDUlVNkBtVeum4tBsSkn6C7hu0dRQtP9AhUK/s400/property.png" width="289" /></a><br />
<br />
<br />
Alternatively, you may also disable this setting using the below Registry keys.<br />
<br />
Registry key:<b> HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers</b><br />
<br />
String: “Path to your application exe”<br />
Value:<b> ~ HIGHDPIAWARE</b><br />
Please note there is a <b>space </b>between ‘tilde’ and ‘HIGHDPIAWARE’.<br />
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjigo7EOtBSee715HHL0q4_mvMiEtfHcYOlFjjFzQZtUirXrRTbVD1LXxpIFHHzVNOB-TIhOr7gbPisq-fck5X5bmx_yo3PrNHakaKcy7_O9Zp-_9IiHMgk4Kl28JqEkj0ykPnwog32kXbS/s1600/regisrty.png"><img border="0" height="26" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjigo7EOtBSee715HHL0q4_mvMiEtfHcYOlFjjFzQZtUirXrRTbVD1LXxpIFHHzVNOB-TIhOr7gbPisq-fck5X5bmx_yo3PrNHakaKcy7_O9Zp-_9IiHMgk4Kl28JqEkj0ykPnwog32kXbS/s400/regisrty.png" width="400" /></a><br />
<br />
There are number of ways to set a value in the registry. Below steps will show the process to set this value in registry using Inno setup:<br />
<br />
var<br />
str_MyAppFullExePath: String;<br />
<br />
str_MyAppFullExePath := ExpandConstant('{app}') ;<br />
<br />
RegWriteStringValue(<b>HKEY_LOCAL_MACHINE</b>, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers', str_MyAppFullExePath + '\' + '{#MyAppExeName}' , '~ HIGHDPIAWARE');<br />
<br />
<b>Possible Issues:</b><br />
<br />
If your application is 32 bit, then the above command will set the value at<br />
<div>
<br /></div>
<div>
“HKEY_LOCAL_MACHINE\SOFTWARE\<b>Wow6432Node</b>\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers” </div>
<div>
<br /></div>
<div>
instead of </div>
<div>
<br /></div>
<div>
“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers”. </div>
<div>
<br /></div>
<div>
This will not set the “Disable Display Scaling on High DPI Settings” property. This can be fixed by using the below command:<br />
<br />
if IsWin64 then<br />
begin<br />
<blockquote class="tr_bq">
RegWriteStringValue(HKLM64, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers', str_MyAppFullExePath + '\' + '{#MyAppExeName}' , '~ HIGHDPIAWARE');</blockquote>
end<br />
else<br />
begin<br />
<blockquote class="tr_bq">
RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers', str_MyAppFullExePath + '\' + '{#MyAppExeName}' , '~ HIGHDPIAWARE');</blockquote>
end ;<br />
<br />
It will force the setup to store value in 64 bit registry.</div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-28393791304474099762015-11-23T05:29:00.001-08:002015-11-23T05:30:27.866-08:00How to extract the associated icon of any file in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
To extract icon of any file, define the below class and the structure:<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct SHFILEINFO<br />
{<br />
public IntPtr hIcon;<br />
public IntPtr iIcon;<br />
public uint dwAttributes;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]<br />
public string szDisplayName;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]<br />
public string szTypeName;<br />
};<br />
<br />
<br />
class Win32<br />
{<br />
public const uint SHGFI_ICON = 0x100;<br />
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon<br />
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon<br />
<br />
[DllImport("shell32.dll")]<br />
public static extern IntPtr SHGetFileInfo(string pszPath,<br />
uint dwFileAttributes,<br />
ref SHFILEINFO psfi,<br />
uint cbSizeFileInfo,<br />
uint uFlags);<br />
<br />
[DllImport("User32.dll")]<br />
public static extern int DestroyIcon(IntPtr hIcon);<br />
}<br />
<br />
Now define the following function:<br />
<br />
SHFILEINFO shinfo = new SHFILEINFO();<br />
/* Define it globally so that you can use the same object for all the files. */<br />
public Bitmap extractAssociatedIcon(string fName)<br />
{<br />
Icon myIcon = null;<br />
try<br />
{<br />
IntPtr hImgSmall; //the handle to the system image list <br />
<br />
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,<br />
(uint)Marshal.SizeOf(shinfo),<br />
Win32.SHGFI_ICON |<br />
Win32.SHGFI_SMALLICON);<br />
<br />
if (shinfo.hIcon != IntPtr.Zero)<br />
myIcon = Icon.FromHandle(shinfo.hIcon); <br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
// Handle any exception here<br />
}<br />
return myIcon.ToBitmap();<br />
}<br />
<br />
<br />
Get the icon by calling the above defined function.<br />
<br />
Bitmap image = extractAssociatedIcon("fullPath to the file");<br />
<br />
Once you get the icon, make sure you destroy the icon. This is very important, else you will run into memory issues.<br />
<br />
<b>Win32.DestroyIcon(shinfo.hIcon);</b><br />
<br />
<br />
<br />
<br /></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-33792266448364930432015-11-20T05:05:00.000-08:002015-11-26T04:03:23.081-08:00Dealing with “Application has stopped responding” issue in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
Suppose in your application, on a click event you are doing
some calculations which is taking a lot of time. During that time, if a user perform unnecessarily clicks on the screen, it might give a “Page is not responding” or "Application has stopped responding" error.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
This can be resolved by following the below steps:<o:p></o:p></div>
<div class="MsoListParagraphCxSpFirst" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraphCxSpFirst" style="margin-bottom: 0.0001pt;">
<!--[if !supportLists]-->1.<span style="font-size: 7pt; font-stretch: normal;"> </span>Put your long running code in a thread.<br /><br />2. Set “Cursor.Current = Cursors.WaitCursor;”<br /><br />3. UseWaitCursor = true;<br /><br />4. Inside the thread use the try-finally block.<br /><br />5. In the finally block, set “this.Cursor = Cursors.Default;”<br /><br />6. UseWaitCursor = false;<br /><br />7. Start that thread.<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><b>Example</b>:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;"> Cursor</span><span style="font-family: Consolas; font-size: 9.5pt;">.Current = <span style="color: #2b91af;">Cursors</span>.WaitCursor;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> UseWaitCursor = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> new</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">Thread</span>(() =><span style="color: blue;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 1in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"> try</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> { <o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">//
Your long running code</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">catch</span>
(<span style="color: #2b91af;">Exception</span> ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;">
// Handle any exception here<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">finally</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> { <span style="color: green;"> </span><span style="color: #2b91af;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 1.5in;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;"> Cursor</span><span style="font-family: Consolas; font-size: 9.5pt;">.Current = <span style="color: #2b91af;">Cursors</span>.Default;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> UseWaitCursor = <span style="color: blue;">false</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }) { Name = <span style="color: #a31515;">"longTask"</span>
}.Start();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Now even if you click
100 times, the page will not show “Not Responding” error.<o:p></o:p></div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-54648584021030186252015-11-18T01:08:00.001-08:002015-11-24T22:01:48.790-08:00Adding a new item to context menu of Windows Explorer in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<b>Requirement</b>: Whenever user right clicks on any folder in windows explorer, display a new option and if user selects that option, launch your application. The below steps will add a new option on right click of any folder:<br />
<br />
You would need to add 2 sub keys in registry at the below location:<br />
<br />
<b>HKEY_CLASSES_ROOT\Folder\shell</b><br />
<br />
<b>Key 1:</b> Folder\\shell\\MyProduct<br />
<br />
Set the value of above key and the same will be shown in the context menu. You can also add a icon to the context menu item by adding a value to the key 1 and name it "Icon" as shown below:<br />
<br />
Icon "Path to your icon file"<br />
<br />
<b>Key 2:</b> Folder\\shell\\MyProduct\\command<br />
<br />
Set the value of above key and it will launch the application:<br />
<br />
Eg. C:\Program Files (x86)\myProduct.exe "%1"<br />
<div>
<br /></div>
<div>
Here "%1" will give the path of the folder which is right clicked.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT7jlFjAgqZ1p655vLlSb0mYIjWH2FT-K0jIZoAnhAUr7PFZBifUMxZ4k6iq7FcW07Mrt2zQWbW6rxvMHlhixmShdj-KU_dJIzWf74vzMFA1ltKyS99QDo5WqC2Pr1V8XKVbfDZMxQFt34/s1600/ContextMenu.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="360" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiT7jlFjAgqZ1p655vLlSb0mYIjWH2FT-K0jIZoAnhAUr7PFZBifUMxZ4k6iq7FcW07Mrt2zQWbW6rxvMHlhixmShdj-KU_dJIzWf74vzMFA1ltKyS99QDo5WqC2Pr1V8XKVbfDZMxQFt34/s640/ContextMenu.png" width="640" /></a></div>
<br />
<br />
<b>Programmatically this can be done as below:</b><br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">private</span><span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">void</span> updateRegistryToAddContextMenu()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">string</span> MenuName = <span style="color: #a31515;">"Folder\\shell\\MyProduct"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">string</span> Command = <span style="color: #a31515;">"Folder\\shell\MyProduct\\command"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: #2b91af;">RegistryKey</span> regmenu = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: #2b91af;">RegistryKey</span> regcmd = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">try</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regmenu = <span style="color: #2b91af;">Registry</span>.ClassesRoot.CreateSubKey(MenuName);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: blue;">if</span> (regmenu != <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regmenu.SetValue(<span style="color: #a31515;">""</span>,<span style="color: #a31515;">"New context menu option"</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regcmd = <span style="color: #2b91af;">Registry</span>.ClassesRoot.CreateSubKey(Command);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: blue;">if</span> (regcmd != <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: blue;">string</span> exePath = <span style="color: #2b91af;">Application</span>.ExecutablePath;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regcmd.SetValue(<span style="color: #a31515;">""</span>, exePath
+ <span style="color: #a31515;">" "</span> + <span style="color: #a31515;">"\""</span>
+ <span style="color: #a31515;">"%1"</span> + <span style="color: #a31515;">"\""</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">catch</span> (<span style="color: #2b91af;">Exception</span>
ex)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: #2b91af;">MessageBox</span>.Show(ex.Message);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> <span style="color: blue;">finally</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: blue;">if</span> (regmenu != <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regmenu.Close();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
<span style="color: blue;">if</span> (regcmd != <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
regcmd.Close();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> }<o:p></o:p></span></div>
</div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-18772690778900081332015-10-01T02:11:00.001-07:002015-10-01T02:11:27.741-07:00Enumerate folder with “No Name” (blank space folder, AKA non-breaking space) in C#.Net:<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
A folder with no name can be created by following the below
steps:<o:p></o:p></div>
<div class="MsoNormal">
</div>
<span style="background: white;"><ol style="text-align: left;">
<li><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;">Right-click on the<span class="apple-converted-space"> </span></span><b style="background-color: transparent;">folder </b><span style="background-color: transparent;">and select Rename.</span></li>
<li>Now press the Alt key and from the Numeric keypad, press
<b>0160</b>.</li>
<li>Now press Enter or click anywhere on the desktop.</li>
</ol>
</span><br />
<div class="MsoNormal">
<span style="background: white;"><br /></span></div>
<div class="MsoNormal">
<span style="background: white;">If you want to retrieve all the files and folders from any
directory (in C#), you would do something like this:<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> path = “Path to the directory”;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">DirectoryInfo</span><span style="font-family: Consolas; font-size: 9.5pt;"> di = new
DirectoryInfo(path);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;"> (DirectoryInfo
dirInfo in di.GetDirectories())<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> FileInfo[] fInfos = </span><span style="font-family: Consolas; font-size: 12.6667px;">dirInfo</span><span style="font-family: Consolas; font-size: 9.5pt;">.GetFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> // You
can easily iterate through the files here<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
But if you have any folder with a blank space as name, the above method
would give you wrong result. You would need to make few changes as shown below:<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> path = “Path to the directory”<span style="background-color: #b6d7a8;"> + <b>“\\”</b></span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">DirectoryInfo</span><span style="font-family: Consolas; font-size: 9.5pt;"> di = new
DirectoryInfo(path);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;"> (DirectoryInfo
dirInfo in di.GetDirectories())</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="background-color: #b6d7a8;">if</span></span><span style="background-color: #b6d7a8;"><span style="font-family: Consolas; font-size: 9.5pt;"> (</span><span style="font-family: Consolas; font-size: 12.6667px;">dirInfo</span><span style="font-family: Consolas; font-size: 9.5pt;">.Name == "\u00A0")<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: #b6d7a8; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: #b6d7a8;"><span style="font-family: Consolas; font-size: 9.5pt;"> string fullPath = </span><span style="font-family: Consolas; font-size: 12.6667px;">dirInfo</span><span style="font-family: Consolas; font-size: 9.5pt;">.FullName + "\\";<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: #b6d7a8;"><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="font-family: Consolas; font-size: 12.6667px;">dirInfo </span><span style="font-family: Consolas; font-size: 9.5pt;">= new DirectoryInfo(fullPath);<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: #b6d7a8;"><span style="font-family: Consolas; font-size: 9.5pt;"> }</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span></span></div>
<div>
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> FileInfo[] fInfos = </span><span style="font-family: Consolas; font-size: 12.6667px;">dirInfo.</span><span style="font-family: Consolas; font-size: 9.5pt;">GetFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> // You
can easily iterate through the files<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white;"><span style="background-color: transparent;"> </span> <span style="color: #222222;"><o:p></o:p></span></span></div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-40546947012563092812015-10-01T01:37:00.002-07:002015-10-01T01:37:51.818-07:00Reduce flickering from controls in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
Flickering effect can be reduced by double buffering the controls. The below code will help you in reducing the flickering effect from any type of control.<br />
<br />
public static class ControlExtensions<br />
{<br />
public static void SetDoubleBuffered(System.Windows.Forms.Control c)<br />
{<br />
if (System.Windows.Forms.SystemInformation.TerminalServerSession)<br />
return;<br />
<br />
System.Reflection.PropertyInfo aProp =<br />
typeof(System.Windows.Forms.Control).GetProperty(<br />
"DoubleBuffered",<br />
System.Reflection.BindingFlags.NonPublic |<br />
System.Reflection.BindingFlags.Instance);<br />
<br />
aProp.SetValue(c, true, null);<br />
}<br />
}<br />
<br />
Whether it is a panel, or a Tab control, you can call the above defined function:<br />
<br />
ControlExtensions.SetDoubleBuffered(MenuPanel);<br />
ControlExtensions.SetDoubleBuffered(settingsTabControl);<br />
<br /></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-55718949357510981932015-09-25T05:09:00.004-07:002015-09-25T05:09:57.064-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
Copy, Move and Delete Files and Folders in C#:<o:p></o:p></h1>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Below are the commonly used methods (using “System.IO”
namespace):<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>For deleting:</b><o:p></o:p></div>
<div class="MsoNormal">
File: File.Delete(“Path to the file”);<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Folder: <span style="font-family: Consolas; font-size: 9.5pt;">Directory</span><span style="font-family: Consolas; font-size: 9.5pt;">.Delete</span>(“Path to the folder”<span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The above methods will delete the file \ folder permanently.
User cannot restore them back.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>For Copying:</b><o:p></o:p></div>
<div class="MsoNormal">
File: File. Copy(“source path” , “destination path”);<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Folder:
<span style="font-family: Consolas; font-size: 9.5pt;">You
will have to recursively traverse through the source directory and create
directory and copy files.</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>For Moving:</b><o:p></o:p></div>
<div class="MsoNormal">
File: File.Move(“source path” , “destination path”);<o:p></o:p></div>
<div class="MsoNormal">
Folder: <span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">Directory.</span>Move(“ source path” ,
“destination path”);<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
The above methods
wouldn’t display the error dialogs, in case of any error OR the progress dialog
to show the progress.<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<h3 style="margin-bottom: 0.0001pt; text-align: left;">
<b>"Microsoft.VisualBasic.FileIO"namespace provides a class “FileSystem”
that shows all the dialog such as Error dialog, Progress dialog etc. and
it’s very simple to use.</b><span style="font-family: Consolas; font-size: 9.5pt;"> </span></h3>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<o:p></o:p></div>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<b>For deleting:</b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
File:
<span style="font-family: Consolas; font-size: 9.5pt;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt;">.DeleteFile</span>(“Path to the file”<span style="font-family: Consolas; font-size: 9.5pt;">, UIOption.OnlyErrorDialogs,
RecycleOption.SendToRecycleBin);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
Folder: <span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">.DeleteDirectory(</span>“Path
to the file”<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">,
UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Here 3<sup>rd</sup> Argument: <span style="font-family: Consolas; font-size: 9.5pt;">RecycleOption also provide 2 options:</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<ul style="text-align: left;">
<li><span style="font-family: Consolas; font-size: 9.5pt; text-indent: -0.25in;">Delete to Recycle Bin.</span></li>
<li><span style="font-family: Consolas; font-size: 9.5pt; text-indent: -0.25in;">Delete permanently.</span></li>
</ul>
<br />
<div class="MsoListParagraphCxSpLast" style="margin: 0in 0in 0.0001pt 0.75in; text-indent: -0.25in;">
<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<b>For Copying:</b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
File: <span style="font-family: Consolas; font-size: 9.5pt;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt;">.CopyFile</span>(“ source path” , “destination
path”<span style="font-family: Consolas; font-size: 9.5pt;">, UIOption.OnlyErrorDialogs</span>);<span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Folder: <span style="font-family: Consolas; font-size: 9.5pt;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt;">.CopyDirectory(sourceFile, destFile, UIOption.OnlyErrorDialogs);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<b>For Moving:</b><o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
File: <span style="font-family: Consolas; font-size: 9.5pt;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt;">.MoveFile(sourcePath, targetPath, UIOption.OnlyErrorDialogs);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Folder: <span style="font-family: Consolas; font-size: 9.5pt;">FileSystem</span><span style="font-family: Consolas; font-size: 9.5pt;">.MoveDirectory(sourcePath, targetPath, UIOption.OnlyErrorDialogs);<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
Argument <b>UIOption</b> passed above provide 2 options:</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<ul style="text-align: left;">
<li><span style="text-indent: -0.25in;">To show only error dialogs</span></li>
<li><span style="font-size: 7pt; font-stretch: normal; text-indent: -0.25in;"> </span><span style="text-indent: -0.25in;">To show all dialogs.</span></li>
</ul>
<br />
<div class="MsoListParagraphCxSpFirst" style="margin-bottom: 0.0001pt; text-align: left; text-indent: -0.25in;">
<o:p></o:p></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoListParagraphCxSpLast" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
<o:p></o:p></div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-68503446225525172102015-07-29T03:49:00.001-07:002015-07-29T03:52:42.359-07:00Get the size of files having long path in C#.Net 3.5<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
</div>
<h2 style="text-align: left;">
<span style="font-size: 14.0pt; line-height: 115%;">Get the size
of files having long path in C#.Net 3.5</span></h2>
<br />
<div style="text-align: left;">
<span style="font-size: 12pt; line-height: 115%;">Suppose you
want to get the size of all the files present in a directory. You would do
something like this:</span></div>
<h4 style="text-align: left;">
<span style="line-height: 18.3999996185303px;">Method 1:</span></h4>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">DirectoryInfo</span><span style="font-family: Consolas; font-size: 9.5pt;"> di = <span style="color: blue;">new</span>
<span style="color: #2b91af;">DirectoryInfo</span>(path); // path: It’s the path
to the directory<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">FileInfo</span><span style="font-family: Consolas; font-size: 9.5pt;">[] fInfos = di.GetFiles();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">long</span><span style="font-family: Consolas; font-size: 9.5pt;"> fileSize = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;"> (<span style="color: #2b91af;">FileInfo</span> fi <span style="color: blue;">in</span> fInfos)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">{ <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt; text-indent: 0.5in;">
<span style="font-family: Consolas; font-size: 9.5pt;">fileSize = fi.Length; <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 12.0pt;">But if you have files having full path (including directory name) more
than 260 characters, “</span><span style="font-family: Consolas; font-size: 9.5pt;">fi.Length</span><span style="font-size: 12.0pt;">” will throw FileNotFoundException. Even if the file
exists, you will get this error, as shown in the screenshot below:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 12.0pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLDpRXdEkcgsJaAGZ9bgaVo4srAcG_4O9C3hlQb9DnlXgUCdXwLmiUNYz5bx-81zkzUEP_UVUJOYmlsqlz14Wy4LDtYijiVO7q53dvELxal4X8bHc8Vo79Oiz156lxFJB6G8m0GJIDRrT3/s1600/error.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="76" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLDpRXdEkcgsJaAGZ9bgaVo4srAcG_4O9C3hlQb9DnlXgUCdXwLmiUNYz5bx-81zkzUEP_UVUJOYmlsqlz14Wy4LDtYijiVO7q53dvELxal4X8bHc8Vo79Oiz156lxFJB6G8m0GJIDRrT3/s400/error.png" width="400" /></a></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-size: 12.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;">To fix this
issue, include the following namespace:</span></div>
<blockquote class="tr_bq">
using System.Reflection;</blockquote>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;">And use the below
code:<o:p></o:p></span></div>
<blockquote class="tr_bq" style="margin-bottom: 0.0001pt;">
public const int MAX_PATH = 260;<br />
public const int MAX_ALTERNATE = 14;<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
public struct FILETIME<br />
{<br />
<blockquote class="tr_bq">
public uint dwLowDateTime;<br />
public uint dwHighDateTime;</blockquote>
};<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]<br />
public struct WIN32_FIND_DATA<br />
{<br />
<blockquote class="tr_bq">
public FileAttributes dwFileAttributes;<br />
public FILETIME ftCreationTime;<br />
public FILETIME ftLastAccessTime;<br />
public FILETIME ftLastWriteTime;<br />
public uint nFileSizeHigh;<br />
public uint nFileSizeLow;<br />
public uint dwReserved0;<br />
public uint dwReserved1;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]<br />
public string cFileName;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_ALTERNATE)]<br />
public string cAlternate;</blockquote>
}</blockquote>
<br />
<blockquote class="tr_bq" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"></span><span style="font-family: Consolas; font-size: 9.5pt;">[<span style="color: #2b91af;">DllImport</span>(<span style="color: #a31515;">"kernel32"</span>, CharSet = <span style="color: #2b91af;">CharSet</span>.Unicode)]</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">static</span> <span style="color: blue;">extern</span> <span style="color: #2b91af;">IntPtr</span>
FindFirstFile(<span style="color: blue;">string</span> lpFileName, <span style="color: blue;">out</span> <span style="color: #2b91af;">WIN32_FIND_DATA</span>
lpFindFileData);<o:p></o:p></span></blockquote>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;">Define the
below function that will return the size of files having long path:<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">long</span> getFileSize(<span style="color: blue;">string</span> fileName, <span style="color: blue;">out</span> <span style="color: #2b91af;">WIN32_FIND_DATA</span> findData)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">long</span> size = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">IntPtr</span> INVALID_HANDLE_VALUE = <span style="color: blue;">new</span> <span style="color: #2b91af;">IntPtr</span>(-1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: #2b91af;">IntPtr</span> findHandle;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
findHandle = FindFirstFile(<span style="color: #a31515;">@"\\?\"</span>
+ fileName , <span style="color: blue;">out</span> findData);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (findHandle != INVALID_HANDLE_VALUE)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> ((findData.dwFileAttributes & <span style="color: #2b91af;">FileAttributes</span>.Directory) == 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
size = (<span style="color: blue;">long</span>)findData.nFileSizeLow + (<span style="color: blue;">long</span>)findData.nFileSizeHigh * 4294967296;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">return</span> size;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="font-family: Consolas; font-size: 9.5pt;">}</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;">Update the
Method 1 with the below code:<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;"><br /></span></div>
<br />
<br />
DirectoryInfo di = new DirectoryInfo(path); // path: It’s the path to the directory <br />
FileInfo[] fInfos = di.GetFiles(); <br />
long fileSize = 0; <br />
if (fi.Exists) <br />
{ <br />
<blockquote class="tr_bq">
fileSize = fi.Length; </blockquote>
} <br />
else <br />
{ <br />
<blockquote class="tr_bq">
// This will handle long paths<br />
FieldInfo fld = typeof(FileSystemInfo).GetField( "FullPath", BindingFlags.Instance | <br />
BindingFlags.NonPublic);<br />
string fullPath = fld.GetValue(fi).ToString();<br />
WIN32_FIND_DATA findData;<br />
fileSize = getFileSize(fullPath, out findData); </blockquote>
}<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 12.0pt; line-height: 115%;">The function
</span><span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">getFileSize
outputs “findData” of type <span style="color: #2b91af;">WIN32_FIND_DATA. </span></span><span style="font-size: 12.0pt; line-height: 115%;">It can be used to get the attribute,
created date, last access date and other properties of file having long path as
below:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> attribute = findData.dwFileAttributes.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">DateTime </span><span style="font-family: Consolas; font-size: 9.5pt;">CreationTime = <span style="color: #2b91af;">DateTime</span>.FromFileTimeUtc((((<span style="color: blue;">long</span>)findData.ftCreationTime.dwHighDateTime) <<
32) | ((<span style="color: blue;">uint</span>)findData.ftCreationTime.dwLowDateTime));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">DateTime </span><span style="font-family: Consolas; font-size: 9.5pt;">LastAccessTime = <span style="color: #2b91af;">DateTime</span>.FromFileTimeUtc((((<span style="color: blue;">long</span>)findData.ftLastAccessTime.dwHighDateTime)
<< 32) | ((<span style="color: blue;">uint</span>)findData.ftLastAccessTime.dwLowDateTime));<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">DateTime </span><span style="font-family: Consolas; font-size: 9.5pt;">LastWriteTime = <span style="color: #2b91af;">DateTime</span>.FromFileTimeUtc((((<span style="color: blue;">long</span>)findData.ftLastWriteTime.dwHighDateTime)
<< 32) | ((<span style="color: blue;">uint</span>)findData.ftLastWriteTime.dwLowDateTime));</span><span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-28520644290617674802015-06-24T23:43:00.003-07:002015-06-25T04:24:21.962-07:00Dealing with PathTooLongException while scanning files with long paths in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
Suppose you have a list of object of class ‘FileInfo’. This class provide properties and instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.</div>
<pre lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, 'Courier New', Courier, mono; font-size: 9pt; font-stretch: normal; overflow: auto; padding: 6px; tab-size: 4;">List<FileInfo> AllFilesInfo;</pre>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
Let say you have a DataTable 'dt' with columns ‘Name’ and ‘Path’. For all the files in AllFilesInfo object, you want to fill in the name and directory name in dataTable.</div>
<pre lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, 'Courier New', Courier, mono; font-size: 9pt; font-stretch: normal; overflow: auto; padding: 6px; tab-size: 4;">foreach (FileInfo fileInfo in AllFilesInfo)
{
DataRow dr = dt.NewRow();
dr[0] = fileInfo.Name;
dr[1] = fileInfo.DirectoryName;
}</pre>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
The above code works well if you don’t have any file with long path. But as soon as, your code finds any such file, you will encounter the below exception:</div>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
<strong style="border: 0px; margin: 0px; padding: 0px;">PathTooLongException was caught: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.</strong></div>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
When you debug and add a watch for fileInfo object, you will find both Directory and DirectoryName are throwing this exception. Now if you expand the ‘Non-public members’ and then the ‘base’ object of ‘fileInfo’, you will see a property named ‘FullPath’ giving the fullpath, which is what you need.</div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
</div>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVNCvFd4dzhwrHSZgKD1f4IQTKrDtW1PBOZP2n8_9DEz4PgB5nMb8sZn2YmtfpNaXKfErSw4B_of8iS_Juz-TfPi_8fHXVqEG92y3z-bnQ4hW08Jx6cLMXUiLqOZwPogc37L_cbpvjZVYY/s1600/Docs.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVNCvFd4dzhwrHSZgKD1f4IQTKrDtW1PBOZP2n8_9DEz4PgB5nMb8sZn2YmtfpNaXKfErSw4B_of8iS_Juz-TfPi_8fHXVqEG92y3z-bnQ4hW08Jx6cLMXUiLqOZwPogc37L_cbpvjZVYY/s320/Docs.png" width="203" /></a></div>
<br />
‘FullPath’ is a protected property of class ‘FileSystemInfo’, which is a base class for both ‘FileInfo’ and ‘DirectoryInfo’. You can access the path as follows:</div>
<pre lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, 'Courier New', Courier, mono; font-size: 9pt; font-stretch: normal; overflow: auto; padding: 6px; tab-size: 4;">DataRow dr = dt.NewRow();
dr[0] = fileInfo.Name;
try
{
dr[1] = fileInfo.DirectoryName;
}
catch (PathTooLongException ex)
{
FieldInfo fld = typeof(FileSystemInfo).GetField(
"FullPath",
BindingFlags.Instance |
BindingFlags.NonPublic);
string fullPath = fld.GetValue(fileInfo).ToString();
fullPath = fullPath.Remove(fullPath.IndexOf(fileInfo.Name)); // Removing file name from the fullpath
dr[1] = fullPath;
}</pre>
<div style="color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">
By using the above code, you can access the fullPath of files with long names.</div>
</div>Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-45767244679138994022015-06-16T23:07:00.000-07:002015-06-16T23:15:11.572-07:00Determining the owner of a file\folder in C#.Net and handling with general exceptions<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<u><span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif; font-size: small;">Determining the owner of a file\folder in C#.Net</span></span></u></h2>
<div style="text-align: left;">
<span style="font-family: Times, Times New Roman, serif; line-height: 115%;">In order to
determine the owner of a file/folder, you would need to use the classes from
the following namespaces:</span></div>
<blockquote class="tr_bq" style="margin-bottom: 0.0001pt;">
<span style="font-family: Times, Times New Roman, serif;">using
System.IO;<br />using
System.Security.AccessControl;<br /><span style="line-height: 115%;">using</span><span style="line-height: 115%;"> </span><span style="line-height: 115%;">System.Security.Principal;<o:p></o:p></span></span></blockquote>
<div class="MsoNormal">
<span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif;"><br /></span></span></div>
<div class="MsoNormal">
<span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif;">Put the following
lines in a function and it will give you the owner name:<o:p></o:p></span></span></div>
<blockquote class="tr_bq" style="margin-bottom: 0.0001pt;">
<span style="font-family: Times, Times New Roman, serif;"><span style="color: #2b91af;">FileSecurity</span> fileSecurity = <span style="color: #2b91af;">File</span>.GetAccessControl(path);<br /><o:p></o:p><span style="color: #2b91af;">IdentityReference</span> sid = fileSecurity.GetOwner(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">SecurityIdentifier</span>));<br /><o:p></o:p><span style="color: #2b91af;">NTAccount</span> ntAccount = sid.Translate(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">NTAccount</span>))
<span style="color: blue;">as</span> <span style="color: #2b91af;">NTAccount</span>;<br /><o:p></o:p>string owner = ntAccount.Value;</span></blockquote>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Times, Times New Roman, serif;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Times, Times New Roman, serif;">Here ‘path’ is the location of the file/folder. </span></div>
<h3 style="text-align: left;">
<b><span style="font-family: Times, Times New Roman, serif; font-size: small;">General Exceptions:</span></b></h3>
<h4 style="text-align: left;">
<span style="font-family: Times, Times New Roman, serif;"><span style="text-indent: -0.25in;">1</span><span style="color: #2b91af; text-indent: -0.25in;">.<span style="font-stretch: normal;">
</span></span><b style="text-indent: -0.25in;"><u>UnauthorizedAccessException</u></b><span style="color: #2b91af; text-indent: -0.25in;">: </span><span style="text-indent: -0.25in;">If you don’t have access to the
file, you will get this exception.</span></span></h4>
<h4 style="margin-bottom: 0.0001pt; text-align: left;">
<span style="font-family: Times, Times New Roman, serif; font-size: small;"><span style="text-indent: -0.25in;">2.<span style="font-stretch: normal;"> </span></span><b style="text-indent: -0.25in;"><u>IdentityNotMappedException</u></b><span style="text-indent: -0.25in;">: “Some or all identity references could not be
translated”</span></span></h4>
<div style="margin-bottom: 0.0001pt; text-align: left;">
<span style="font-family: Times, Times New Roman, serif; text-indent: 0.5in;"><br /></span></div>
<div style="margin-bottom: 0.0001pt; text-align: left;">
<span style="font-family: Times, Times New Roman, serif;"><span style="text-indent: 0.5in;">Whenever any user is created on the machine, a unique
security ID is assigned. This SID can be converted to Domain Name\Username
format by translating the SID to NTAccount type, but if that user is removed
from the machine then only SID exists. Now if you try to convert the SID to
NTAccount type, the above exception would occur. </span><span style="text-indent: 0.5in;">In that case, you may just want to display the SID as
follows:</span><span style="text-indent: 0.5in;"> </span></span></div>
<blockquote class="tr_bq">
<blockquote class="tr_bq">
IdentityReference sid = null;<br /> string owner = null;<br /> try<br /> {<br /><blockquote class="tr_bq">
FileSecurity fileSecurity = File.GetAccessControl(path);<br /> sid = fileSecurity.GetOwner(typeof(SecurityIdentifier));<br /> NTAccount ntAccount = sid.Translate(typeof(NTAccount)) as NTAccount;<br /> owner = ntAccount.Value;</blockquote>
}<br /> catch (IdentityNotMappedException ex)<br /> {<br /><blockquote class="tr_bq">
if (sid != null)<br /> owner = sid.ToString();</blockquote>
}</blockquote>
</blockquote>
<h3 style="text-align: left;">
<b><span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif;">Optimizing the code if you want to display
the owner name of large number of files :</span></span></b></h3>
<div class="MsoNormal">
<span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif;">If you have
too many files, getting the SIDs and translating it to DomainName\username(s) would
be very time consuming. You can optimize the code by finding the SIDs for all
the files and once you have all the SIDs, translate them to DomainName\username.</span></span></div>
<span style="font-family: Times, Times New Roman, serif;"><br /></span>
<div class="MsoNormal">
<span style="line-height: 115%;"><span style="font-family: Times, Times New Roman, serif;">So, for example: If you
have 10000 files, determine the SID for all the 10000 files. Let say you get 7 SIDs.
Now translate only those 7 SIDs to DomainName\username format. It will definitely
speed up the process.</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com1tag:blogger.com,1999:blog-7424628290310913618.post-14855629620523983472014-10-10T01:13:00.004-07:002014-10-10T01:20:35.337-07:00Exporting datagrid to CSV using Save Dialog in C#.NET<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Helvetica; text-align: left;">
Below is the code for exporting the data of a data grid in C#.Net<br />
<br />
Datagrid is as follows:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtH5dvpxG_kPW2thyphenhyphenNdPS1qbEbnGslo7yF7lx43cHmZqLfTsuQnohj8_npPCNo9l3gM5PgbXh3dIK8jZ4SrDj_Tr0Vy3p5SmMyOkHxLwukwxoDdIVhqQdI1Zb_dnFm8nODgSBjJ3uGhVw5/s1600/Screen+Shot+2014-10-10+at+1.47.45+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtH5dvpxG_kPW2thyphenhyphenNdPS1qbEbnGslo7yF7lx43cHmZqLfTsuQnohj8_npPCNo9l3gM5PgbXh3dIK8jZ4SrDj_Tr0Vy3p5SmMyOkHxLwukwxoDdIVhqQdI1Zb_dnFm8nODgSBjJ3uGhVw5/s1600/Screen+Shot+2014-10-10+at+1.47.45+pm.png" height="199" width="320" /></a></div>
<br /></div>
<div style="font-family: Helvetica; text-align: left;">
<br /></div>
<div style="font-family: Helvetica; text-align: left;">
System.Windows.Forms.<span style="color: #33a2bd;">SaveFileDialog</span> saveDlg = <span style="color: #0433ff;">new</span> System.Windows.Forms.<span style="color: #33a2bd;">SaveFileDialog</span>();</div>
<div style="font-family: Helvetica; text-align: left;">
saveDlg.InitialDirectory = <span style="color: #b4261a;">@"C:\"</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
saveDlg.Filter = <span style="color: #b4261a;">"CSV files (*.csv)|*.csv"</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
saveDlg.FilterIndex = 0;</div>
<div style="font-family: Helvetica; text-align: left;">
saveDlg.RestoreDirectory = <span style="color: #0433ff;">true</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
saveDlg.Title = <span style="color: #b4261a;">"Export csv File To"</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">if</span> (saveDlg.ShowDialog() == System.Windows.Forms.<span style="color: #33a2bd;">DialogResult</span>.OK)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">string</span> CsvFpath = saveDlg.FileName;</div>
<div style="font-family: Helvetica; text-align: left;">
System.IO.<span style="color: #33a2bd;">StreamWriter</span> csvFileWriter = <span style="color: #0433ff;">new</span> <span style="color: #33a2bd;">StreamWriter</span>(CsvFpath, <span style="color: #0433ff;">false</span>);</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">string</span> columnHeaderText = <span style="color: #b4261a;">""</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">int</span> countColumn = comparisonGrid.Columns.Count - 1;</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">if</span> (countColumn >= 0)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
columnHeaderText = (comparisonGrid.Columns[0].Header).ToString();</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; min-height: 11px; text-align: left;">
<br /></div>
<div style="color: #008f00; font-family: Helvetica; text-align: left;">
<span style="color: black;"> </span>// Writing column headers</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">for</span> (<span style="color: #0433ff;">int</span> i = 1; i <= countColumn; i++)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
columnHeaderText = columnHeaderText + <span style="color: #b4261a;">','</span> + (comparisonGrid.Columns[i].Header).ToString();</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; text-align: left;">
csvFileWriter.WriteLine(columnHeaderText);</div>
<div style="font-family: Helvetica; min-height: 11px; text-align: left;">
<br /></div>
<div style="color: #008f00; font-family: Helvetica; text-align: left;">
<span style="color: black;"> </span>// Writing values row by row</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">for</span> (<span style="color: #0433ff;">int</span> i = 0; i <= comparisonGrid.Items.Count - 2; i++)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">string</span> dataFromGrid = <span style="color: #b4261a;">""</span>;</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">for</span> (<span style="color: #0433ff;">int</span> j = 0; j <= comparisonGrid.Columns.Count - 1; j++)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">if</span> (j == 0)</div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
dataFromGrid = ((<span style="color: #33a2bd;">DataRowView</span>)comparisonGrid.Items[i]).Row.ItemArray[j].ToString();</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; text-align: left;">
<span style="color: #0433ff;">else</span></div>
<div style="font-family: Helvetica; text-align: left;">
{</div>
<div style="font-family: Helvetica; text-align: left;">
dataFromGrid = dataFromGrid + <span style="color: #b4261a;">','</span> + ((<span style="color: #33a2bd;">DataRowView</span>)comparisonGrid.Items[i]).Row.ItemArray[j].ToString();</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; text-align: left;">
csvFileWriter.WriteLine(dataFromGrid);</div>
<div style="font-family: Helvetica; text-align: left;">
}</div>
<div style="font-family: Helvetica; text-align: left;">
csvFileWriter.Flush();</div>
<div style="font-family: Helvetica; text-align: left;">
csvFileWriter.Close();</div>
<div style="text-align: left;">
</div>
<div style="font-family: Helvetica; text-align: left;">
}<br />
<br />
The content of output csv file looks like:<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim6BMJQuugBwc9pHqfokjjrt9-6mL0r2BsFYzS3tSiiR-Z0SlZRTLZI1X1JBBms1ktC42BxPf4O0gatJah66YshLhgjFwiDbD-JbAUXSljNCHnHKKCmjn75hrGmMwY0LzSvAo9kFHqKQzT/s1600/Screen+Shot+2014-10-10+at+1.48.18+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim6BMJQuugBwc9pHqfokjjrt9-6mL0r2BsFYzS3tSiiR-Z0SlZRTLZI1X1JBBms1ktC42BxPf4O0gatJah66YshLhgjFwiDbD-JbAUXSljNCHnHKKCmjn75hrGmMwY0LzSvAo9kFHqKQzT/s1600/Screen+Shot+2014-10-10+at+1.48.18+pm.png" height="185" width="400" /></a></div>
<br /></div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-33898258312147595372014-10-09T01:27:00.001-07:002014-10-09T01:35:46.318-07:00Exporting data grid to Excel with Save Dialog box in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">I’ve the following data grid (say comparisonGrid) and I'll export this data into excel by following the below steps.</span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjx3TYXzWPFMDWKvdwlsmzujfH8dlbGrarroXj676Xnb2AvUjioJvOHW73uihaMvL0PJmkVbrbBf-Nh6eeLcengjDEI2p_aMkH5kicdGJbULCvpx6VbsVRnXjxlx2umCeZ0Dm-_sCo6rfaJ/s1600/Screen+Shot+2014-10-09+at+12.29.41+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjx3TYXzWPFMDWKvdwlsmzujfH8dlbGrarroXj676Xnb2AvUjioJvOHW73uihaMvL0PJmkVbrbBf-Nh6eeLcengjDEI2p_aMkH5kicdGJbULCvpx6VbsVRnXjxlx2umCeZ0Dm-_sCo6rfaJ/s1600/Screen+Shot+2014-10-09+at+12.29.41+pm.png" height="132" width="320" /></span></a></div>
<div class="separator" style="clear: both; text-align: center;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Step 1: Include the following namespace to your code behind file (.cs file)<br />using Excel = Microsoft.Office.Interop.Excel;</span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br />Step 2: Now you would need to instantiate the instances of Excel application, excel workbook and worksheet as below (I'll do that in the export button click event):<br /> Excel.<span style="color: #33a2bd;">Application</span> xlApp;<br /> Excel.<span style="color: #33a2bd;">Workbook</span> xlWorkBook;<br /> Excel.<span style="color: #33a2bd;">Worksheet</span> xlWorkSheet;<br /> <span style="color: #0433ff;">object</span> misValue = System.Reflection.<span style="color: #33a2bd;">Missing</span>.Value;<br /> xlApp = <span style="color: #0433ff;">new</span> Excel.<span style="color: #33a2bd;">ApplicationClass</span>();<br /> xlWorkBook = xlApp.Workbooks.Add(misValue);<br /> xlWorkSheet = (Excel.<span style="color: #33a2bd;">Worksheet</span>)xlWorkBook.Worksheets.get_Item(1);</span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br />Step 3: Fill in the excel sheet with the values of the data grid (cell by cell) as follows:<br /><span style="color: #0433ff;"> for</span> (<span style="color: #0433ff;">int</span> i = 0; i <= comparisonGrid.Items.Count - 1; i++)<br /> {<br /> <span style="color: #0433ff;">for</span> (<span style="color: #0433ff;">int</span> j = 0; j <= comparisonGrid.Columns.Count - 1; j++)<br /> {<br /> xlWorkSheet.Cells[i + 1, j + 1] = <br /> ( (<span style="color: #33a2bd;">DataRowView</span>)comparisonGrid.Items[i]).Row.ItemArray[j].ToString();<br /> }<br /> }</span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br />Step 4: For saving the file using “Save dialog” box, you would add the following code:<br /><span style="color: #0433ff;"> </span></span></div>
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> System.Windows.Forms.<span style="color: #33a2bd;">SaveFileDialog</span> saveDlg = <span style="color: #0433ff;">new</span> System.Windows.Forms.<span style="color: #33a2bd;">SaveFileDialog</span>();</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> saveDlg.InitialDirectory = <span style="color: #b4261a;">@"C:\"</span>;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> saveDlg.Filter = <span style="color: #b4261a;">"Excel files (*.xls)|*.xls"</span>;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> saveDlg.FilterIndex = 0;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> saveDlg.RestoreDirectory = <span style="color: #0433ff;">true</span>;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> saveDlg.Title = <span style="color: #b4261a;">"Export Excel File To"</span>;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="color: #0433ff;"> if</span> (saveDlg.ShowDialog() == System.Windows.Forms.<span style="color: #33a2bd;">DialogResult</span>.OK)</span></div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> {</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> <span style="color: #0433ff;">string</span> path = saveDlg.FileName;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> xlWorkBook.SaveCopyAs(path);</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> xlWorkBook.Saved = <span style="color: #0433ff;">true</span>;</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> xlWorkBook.Close(<span style="color: #0433ff;">true</span>, misValue, misValue);</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> xlApp.Quit();</span><br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"> }</span><br />
<div>
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div style="text-align: left;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span style="background-color: white; line-height: 17.804800033569336px;">You may encounter the below <span style="color: red;">error</span>:</span><span style="background-color: white; line-height: 17.804800033569336px;"><br /></span><span style="background-color: white; line-height: 17.804800033569336px;">Error 1 Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.</span><span style="background-color: white; line-height: 17.804800033569336px;"><br /></span><span style="background-color: white; line-height: 17.804800033569336px;">Fix:</span>In your Project, expand the "References", find the Microsoft Office Interop reference. Right click it and select properties, and change "Embed Interop Types" to <code>false</code>. Add the following refrence “<span style="color: #0433ff;">using</span> Excel = Microsoft.Office.Interop.Excel;”</span></div>
<div style="text-align: left;">
<div>
<div style="font-family: Helvetica; font-size: 9.5px;">
</div>
<div style="text-align: left;">
</div>
</div>
<div>
<div>
<div style="text-align: left;">
</div>
<blockquote style="border: none; margin: 0px 0px 0px 40px; padding: 0px; text-align: left;">
</blockquote>
</div>
</div>
</div>
<div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>
</div>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-8205046705184378122014-08-04T03:04:00.002-07:002014-08-04T03:04:18.324-07:00Creating WCF Client from Service Metadata<div dir="ltr" style="text-align: left;" trbidi="on">
Below are the steps to create WCF clients from service metadata using Visual Studio 2013:<br />
<br />
<i>I'm assuming you've a WCF service created and running on WCF Test client.</i><br />
<br />
1. Open Visual Studio command prompt. You can find in under Start Menu -> All Programs -> Visual Studio Tools. If you're working on Windows 7, please make sure you "Run as administrator".<br />
2. Type in the below command:<br />
<br />
svcutil.exe /out:<OutputFileName>.cs /config:<configFileName>.config <Service URL><br />
<br />
<i>For example:</i><br />
If the service url is <u>http://localhost:54225/TestService.svc</u><br />
<u><br /></u>
<i>Type in the below command:</i><br />
svcutil.exe /out:WCFGeneratedProxy.cs /config:output.config http://localhost:54225/TestService.svc<br />
<br />
This will generate the proxy file and an output.config file. Add WCFGeneratedProxy.cs file to your project and add the content of output.config file to the web.config file.<br />
<br />
<br /></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-21760950385408600972014-01-12T03:52:00.000-08:002014-01-12T03:52:56.499-08:00Working with LINQ to SQL<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div>
Creating an ASP.NET website and working with LINQ</div>
<div>
<br /></div>
<div>
You can use LINQ to XML for following purposes-</div>
<div>
<br /></div>
<div>
1.<span class="Apple-tab-span" style="white-space: pre;"> </span>For creating xml file.</div>
<div>
2.<span class="Apple-tab-span" style="white-space: pre;"> </span>For loading an xml file.</div>
<div>
3.<span class="Apple-tab-span" style="white-space: pre;"> </span>For querying xml data.</div>
<div>
4.<span class="Apple-tab-span" style="white-space: pre;"> </span>Manipulating xml data.</div>
<div>
5.<span class="Apple-tab-span" style="white-space: pre;"> </span>Transforming the xml into another shape.</div>
<div>
<br /></div>
<div>
In this example we will load an already created xml file. We will use Contacts.xml file. The content of this file is as shown below-</div>
<div>
<br /></div>
<div>
<contacts></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><id> 1 </id></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><name> Name1 </name></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><department> IT </department></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><id> 2 </id></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><name> Name2 </name></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><department> IT </department></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><id> 3 </id></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><name> Name3 </name></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><department> CS </department></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><contact></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><id> 4 </id></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><name> Name4 </name></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><department> CS </department></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></contact></div>
<div>
</contacts><span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<br /></div>
<div>
Follow the below steps to query through the xml file-</div>
<div>
<br /></div>
<div>
1.<span class="Apple-tab-span" style="white-space: pre;"> </span>Open Visual Studio and create an ASP.NET website called Linq2Xml by using C#.</div>
<div>
2.<span class="Apple-tab-span" style="white-space: pre;"> </span>Add the above created Contacts.xml file to the App_Code directory of your solution.</div>
<div>
3.<span class="Apple-tab-span" style="white-space: pre;"> </span>Add a new class file called Contact.cs to the solution. This class represents an contact object and will be used when the xml is turned to a collection of strongly typed objects. The following code shows an example-</div>
<div>
<br /></div>
<div>
namespace Linq2Xml</div>
<div>
{</div>
<div>
public class Contact</div>
<div>
{</div>
<div>
public int id { get; set; }</div>
<div>
public string name { get; set; }</div>
<div>
public string department { get; set; }</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
4. Add a new class file called ContactServices.cs. This class will expose methods that use LINQ to work with the xml file. Add following code to this file.</div>
<div>
<br /></div>
<div>
i. Add a class level variable to read the class file. Also, add the reference for namespace System.Xml.Linq.</div>
<div>
<br /></div>
<div>
using System;</div>
<div>
using System.Collections.Generic;</div>
<div>
using System.Linq;</div>
<div>
using System.Web;</div>
<div>
using System.Xml.Linq;</div>
<div>
<br /></div>
<div>
namespace Linq2Xml</div>
<div>
{</div>
<div>
public class ContactServices</div>
<div>
{</div>
<div>
XElement _conXml = XElement.Load("c:\\....\\visual studio 2010\\Projects\\Linq2Xml\\Linq2Xml\\App_Data\\Contacts.xml");</div>
<div>
. . . </div>
<div>
<br /></div>
<div>
// define methods here</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
ii. Add a method called GetDepartments to return the distinct departments from the xml file.</div>
<div>
<br /></div>
<div>
public List<string> GetDepartments()</div>
<div>
{</div>
<div>
var deptQuery =</div>
<div>
from con in _conXml.Descendants("contact")</div>
<div>
group con by con.Element("department").Value</div>
<div>
into conGroup</div>
<div>
select conGroup.First().Element("department").Value;</div>
<div>
return deptQuery.ToList();</div>
<div>
}</div>
<div>
</div>
<div>
iii. Add another method called GetContactsByDept that takes department as a parameter. It will then query the xml file and returns department with a matching name. The query will transform data into a list of Contact object.</div>
<div>
<br /></div>
<div>
public List<Contact> GetContactsByDept(string department)</div>
<div>
{</div>
<div>
IEnumerable<Contact> conQuery =</div>
<div>
from con in _conXml.Descendants("contact")</div>
<div>
where con.Element("department").Value == department</div>
<div>
select new Contact</div>
<div>
{</div>
<div>
id = Convert.ToInt32(con.Element("id").Value),</div>
<div>
department = con.Element("department").Value,</div>
<div>
name = con.Element("name").Value,</div>
<div>
};</div>
<div>
return conQuery.ToList();</div>
<div>
}</div>
<div>
<br /></div>
<div>
5. Open the default.aspx page. Add a label, Dropdown list, Button, GridView and ObjectDataSource controls to the page.</div>
<div>
<br /></div>
<div>
6. Configure the ObjectDataSource to point to ContactServices.GetDepartments method. </div>
<div>
<br /></div>
<div>
7. Configure the dropdown list to use the ObjectDataSource.</div>
<div>
<br /></div>
<div>
The markup inside the MainContent placeholder should look similar to the following-</div>
<div>
<br /></div>
<div>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"></div>
<div>
<asp:Label ID="Label1" runat="server" Text="Select a department:"></asp:Label></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<asp:DropDownList ID="DropDownListDepts" runat="server" DataSourceID="ObjectDataSourceDept"></div>
<div>
</asp:DropDownList></div>
<div>
&nbsp;</div>
<div>
<asp:Button ID="Button1" runat="server" Text="Update " onclick="Button1_Click" /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<asp:GridView ID="GridViewContacts" runat="server"></div>
<div>
</asp:GridView></div>
<div>
<br /></div>
<div>
<asp:ObjectDataSource ID="ObjectDataSourceDept" runat="server" SelectMethod="GetDepartments"</div>
<div>
TypeName="Linq2Xml.ContactServices"></asp:ObjectDataSource></div>
<div>
</asp:Content></div>
<div>
<br /></div>
<div>
8. Add a click event to the button control. Inside the code for the click event, call the ContactServices. GetContactsByDept method and pass the selected method from the dropdown list. Update the data in the gridView control with the results. The following code shows an example-</div>
<div>
protected void Button1_Click(object sender, EventArgs e)</div>
<div>
{</div>
<div>
ContactServices conSrv = new ContactServices();</div>
<div>
GridViewContacts.DataSource =</div>
<div>
conSrv.GetContactsByDept(DropDownListDepts.SelectedItem.Text);</div>
<div>
GridViewContacts.DataBind();</div>
<div>
}</div>
<div>
<br /></div>
<div>
9. Run your application. Select a department and click on Update button. Your result should look similar to the below figure.</div>
<div>
<br /></div>
<div>
<img src="webkit-fake-url://D6D5D9B0-D476-4465-9062-0A6CE3D151CB/image.tiff" /><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyXOjkdPuFGrBUZ3vrCwABadlV1BOdWyv8BkSasQYQsyl_NsrI42WMQnAITYo1-9F7D34PVIbLMxcZ_jYny8okHioJNt8l0ir2c26G38plJkVVwXvFPZ2vUVLHbn5v2Kif8enpy90BAXFq/s1600/Screen+Shot+2014-01-08+at+3.34.57+pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyXOjkdPuFGrBUZ3vrCwABadlV1BOdWyv8BkSasQYQsyl_NsrI42WMQnAITYo1-9F7D34PVIbLMxcZ_jYny8okHioJNt8l0ir2c26G38plJkVVwXvFPZ2vUVLHbn5v2Kif8enpy90BAXFq/s1600/Screen+Shot+2014-01-08+at+3.34.57+pm.png" height="175" width="320" /></a></div>
</div>
<div>
<br /></div>
</div>
<div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a>
</div>Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-32166440297872490182013-12-02T08:26:00.000-08:002013-12-02T08:27:23.519-08:00Creating AJAX enabled web forms<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif; font-size: small;"><b>Creating AJAX enabled web forms</b></span></h2>
<div style="text-align: left;">
<ol>
<li><span style="font-family: Georgia, Times New Roman, serif;">AJAX is a platform-independent, ECMAScript-compliant technology for communicating between code running on the client and code running on the server.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">ASP.NET includes both a set of server controls for working with AJAX and a set of client-side JavaScript files called the Microsoft AJAX Library.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">The ScriptManager control is required on all pages that work with the AJAX Extensions for ASP.NET. It manages the JavaScript files sent to the client and the communication between the server and the client.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">The ScriptManagerProxy control is used on pages that work with a master page that already defines a ScriptManager control or with user controls that will be used on pages that include a ScriptManager control.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">The UpdatePanel control allows you to define an area within your page that can post back to the server and receive updates independent of the rest of the page.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">The UpdateProgress control is used to provide notice to the user that the page has initiated a callback to the server.</span></li>
</ol>
<div>
<div class="page" title="Page 489">
<div class="layoutArea">
<h3 style="text-align: left;">
<b><span style="font-family: Georgia, Times New Roman, serif; font-size: small;">Uses and Benefits of ASP.NET AJAX</span></b></h3>
<br />
<ol>
<li><span style="font-family: Georgia, Times New Roman, serif;">Partial-page updates </span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Client-side processing </span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Desktop-like UI </span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Progress indication improved performance and higher scale</span></li>
</ol>
<h3 style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif; font-size: small;"><b>Building an AJAX enabled webpage:</b><b><br /></b></span></h3>
<h3 style="text-align: left;">
<b><span style="font-family: Georgia, Times New Roman, serif; font-size: small;">Enabling Partial page updates:</span></b></h3>
<br />
<div class="page" title="Page 496">
<div class="layoutArea">
<br />
<ol style="text-align: left;">
<li><span style="font-family: Georgia, Times New Roman, serif;">Open Visual Studio and create a new ASP.NET website.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Add SQL Server database to the App_Data folder of the site or you can add northwnd.mdf database file.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Add a table to the above added database. For Eg. Create a table “Employees” with columns “EmployeeID” and “EmployeeName” and add entries to these columns.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Open Default.aspx in Source view. Add a ScriptManager control to the body of the page from the AJAX Extensions tab of the Toolbox. </span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Add text to the page to serve as a title followed by a horizontal line. Your markup inside the BodyContent control might look as follows.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Add an AJAX UpdatePanel control to the page from the AJAX Extensions tab of the Toolbox.</span></li>
</ol>
<div>
<pre style="background-color: #f3f3f3;"><span style="font-family: Georgia, Times New Roman, serif;"><asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
</span></pre>
<pre style="background-color: #f3f3f3;"><span style="font-family: Georgia, Times New Roman, serif;"> <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</span></pre>
<pre style="background-color: #f3f3f3;"><span style="font-family: Georgia, Times New Roman, serif;"> <h2>Employees</h2>
<hr />
</span></pre>
<pre style="background-color: #f3f3f3;"><span style="font-family: Georgia, Times New Roman, serif;"></asp:Content> </span></pre>
</div>
</div>
</div>
<div style="text-align: left;">
</div>
<ol start="7" style="text-align: left;">
<li><span style="font-family: Georgia, Times New Roman, serif;">Switch to Design view and add a GridView control inside the UpdatePanel and bind the GridView to the database table.</span></li>
<li><span style="font-family: Georgia, Times New Roman, serif;">Again, open the GridView Tasks window by using the smart tag. Select the Enable Paging check box.</span></li>
</ol>
<br />
<div style="text-align: left;">
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"><asp:UpdatePanel ID="UpdatePanel1" runat="server"></span></div>
<div style="text-align: left;">
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <Triggers></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <asp:AsyncPostBackTrigger ControlID="ButtonSave" EventName="Click" /></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> </Triggers></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <ContentTemplate></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"</span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> AllowPaging="True"></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <Columns></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" SortExpression="Employee ID" /></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" SortExpression="Employee Name" /></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> </Columns></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> </asp:GridView></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"</span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> </ContentTemplate></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"> </asp:UpdatePanel></span><br />
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">9. Run the application and view the Default.aspx page in a browser. Click the data page numbers to move between data pages. Notice that only the grid is being updated and not the entire page; this is because the UpdatePanel control wraps requests for data and replaces the markup for the grid rather than refreshing the entire page.</span></div>
<div>
<div class="page" title="Page 497">
<div class="layoutArea">
<div class="column">
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;">10. Next, add a section at the top of the form (outside of the UpdatePanel) that allows the user to enter a new employee and have it added to the database. Your markup code might look as follows.</span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div style="text-align: left;">
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"><div style="margin: 20px 0px 20px 40px"><br /> Employee Name<br /> <br /><br /> <asp:TextBox ID="TextBoxName" runat="server" Width="200"></asp:TextBox><br /> <br /><br /> <asp:Button ID="ButtonSave" runat="server" Text="Add" Style="margin-top: 15px" OnClick="ButtonSave_Click" /><br /> </div></span></div>
<div style="text-align: left;">
<span style="background-color: #eeeeee; font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;">11. Next, add a Click event to the ButtonSave button defined in step 11 (onclick="ButtonSave_ Click"). This Click event will add employee name to theEmployees table in the database. At the end of this event, rebind the GridView control. The following code shows an example.</span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div style="text-align: left;">
<span style="background-color: #f3f3f3; font-family: Georgia, Times New Roman, serif;">protected void ButtonSave_Click(object sender, EventArgs e)<br /> {<br /> SqlConnection con = new SqlConnection(conStr);<br /> string qry = "Insert into Employee(EmployeeName) Values('" +TextBoxName.Text + "')";<br /> SqlCommand cmd = new SqlCommand(qry, con);<br /> con.Open();<br /> cmd.ExecuteNonQuery();<br /> con.Close();<br /> GridView1.DataBind();<br /> }</span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;">12. Run the application and enter a row in the table. Notice that the entire page refreshes. Add behavior to the page so that the Add button triggers a partial-page update to the GridView control. To do so, add a trigger to the UpdatePanel control and connect the trigger to the ButtonEnter control. The following markup shows an example.</span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><span style="background-color: #eeeeee;"><Triggers><br /> <asp:AsyncPostBackTrigger ControlID="ButtonSave" EventName="Click" /><br /></Triggers></span></span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<div style="text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;">13. Run the application again and notice that now only GridView updates when a new row is added.</span></div>
<span style="font-family: Georgia, Times New Roman, serif;"></span><br />
<br />
<div class="page" title="Page 499">
<div class="layoutArea">
<br />
<span style="color: #333333;"><span style="font-family: Georgia, Times New Roman, serif;"><br /></span></span>
<span style="color: #333333;"><b><span style="font-family: Georgia, Times New Roman, serif;">Adding a Progress Indicator: </span></b></span><br />
<div style="text-align: left;">
<div title="Page 499">
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div title="Page 499">
<span style="font-family: Georgia, Times New Roman, serif;">1. Open Default.aspx in Source view. Add an UpdateProgress control to the UpdatePanel. Add the control to the bottom of the panel just inside the ContentTemplate element.</span></div>
<div title="Page 500">
<span style="font-family: Georgia, Times New Roman, serif;">2. Add text inside the ProgressTemplate elements of the UpdateProgress control to notify the user that processing is happening on the server. The following shows a sample markup.</span></div>
<div title="Page 500">
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div title="Page 500">
<span style="font-family: Georgia, Times New Roman, serif;"><span style="background-color: #eeeeee;"><asp:UpdateProgress ID="UpdateProgress1" runat="server"></span><br /><span style="background-color: #eeeeee;"> <ProgressTemplate></span><br /><span style="background-color: #eeeeee;"> <div style="margin-top: 20px; font-size: larger; color: Green"></span><br /><span style="background-color: #eeeeee;"> Processing, please wait ...</span><br /><span style="background-color: #eeeeee;"> </div></span><br /><span style="background-color: #eeeeee;"> </ProgressTemplate></span><br /><span style="background-color: #eeeeee;"></asp:UpdateProgress></span></span></div>
<div title="Page 500">
<span style="font-family: Georgia, Times New Roman, serif;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<div title="Page 500">
<span style="font-family: Georgia, Times New Roman, serif;">3. The processing happens pretty fast. Therefore, add a line of code to the end of the ButtonSave_Click event to pause the server-side processing. You can simply put the thread to sleep for a few seconds. The following code shows an example.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<br />
<div style="text-align: left;">
<span style="background-color: #eeeeee;"><span style="font-family: Georgia, Times New Roman, serif;">System.Threading.Thread.Sleep(2000);</span></span></div>
<div style="text-align: left;">
<span style="background-color: #eeeeee;"><span style="font-family: Georgia, Times New Roman, serif;"><br /></span></span></div>
<span style="font-family: Georgia, Times New Roman, serif;">4. Run the application and notice that the notification is shown to the user when you enter a new record in the GridView control.</span></div>
</div>
</div>
</div>
<div>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span></div>
<div class="page" title="Page 498">
<div class="layoutArea">
<div class="column">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com0tag:blogger.com,1999:blog-7424628290310913618.post-39417096989757751602013-10-28T21:08:00.002-07:002013-10-28T21:08:44.664-07:00Adding user control and defining it's functionality in C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;">Advantages of using a User control:</span></h2>
<ol>
<li><span style="font-family: Times, Times New Roman, serif;">User controls provide an easy way to combine several controls into a single unit of functionality that can be dragged onto multiple webpages in the same site.</span></li>
<li><span style="font-family: Times, Times New Roman, serif;">User controls in ASP.NET are created as ASCX files. An ASCX file is similar to the webpage’s ASPX file and can have its own code-behind page.</span></li>
<li><span style="font-family: Times, Times New Roman, serif;">It helps you define a consistent user interface. </span></li>
</ol>
<div title="Page 363">
<div style="orphans: 2; text-align: -webkit-auto; widows: 2;">
<h3>
<span style="font-family: Times, Times New Roman, serif; font-size: small;">You can add user control to your website and define it's events by following the below steps:</span></h3>
</div>
<div style="orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<h3>
<ul>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">Creating the website and the user control:</span></li>
</ul>
</h3>
<div style="orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<ol style="line-height: 15px; orphans: 2; widows: 2;">
<li><span style="font-family: Times, Times New Roman, serif;">Open Visual studio and create a website.</span></li>
<li><span style="font-family: Times, Times New Roman, serif; font-size: small;">Right click the solution explorer and add New item dialog box and select Web User control.</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">This adds a file with the .ascx extension to your application. The user control has both a Design view and a Source view, similar to that of an ASPX page.</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">You can add controls on to the user control in a similar manner as you do for any web page (You can add text boxes, buttons etc).</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">You may now need to define user control events. For example: You have a "Save" button on the design and you want to save the content of the text box when user click on that button.</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; font-size: small; text-align: -webkit-auto;">To do so, you can add a button click event and an event handler to the user control. You can use this event to</span><span style="font-family: Times, 'Times New Roman', serif; line-height: 16px; text-align: -webkit-auto;"> raise the event to the host of the user control</span><span style="font-family: Times, 'Times New Roman', serif; font-size: small; text-align: -webkit-auto;">. Following code shows how to add an event and event handler to a user control file:</span></li>
</ol>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
<span style="font-family: Times, Times New Roman, serif; font-size: small;"></span></div>
<div class="page" style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;" title="Page 382">
<div class="layoutArea">
<div class="column">
<pre><span style="font-family: Times, Times New Roman, serif;">public event EventHandler SaveButtonClick;</span></pre>
</div>
</div>
</div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
<span style="font-family: Times, Times New Roman, serif; font-size: small;"></span></div>
<div class="page" style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;" title="Page 383">
<span style="font-family: Times, Times New Roman, serif; font-size: small;"></span><br />
<div class="section" style="background-color: #f3f3f3;">
<div class="layoutArea">
<pre><span style="font-family: Times, Times New Roman, serif; font-size: small;">protected void ButtonSave_Click(object sender, EventArgs e)
{
</span></pre>
<pre><span style="font-family: Times, Times New Roman, serif; font-size: small;"> if (SaveButtonClick != null)
{
</span></pre>
<pre><span style="font-family: Times, Times New Roman, serif; font-size: small;"> SaveButtonClick(this, new EventArgs());
}</span></pre>
<pre><span style="font-family: Times, Times New Roman, serif; font-size: small;">}</span></pre>
</div>
</div>
</div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<h3>
<ul>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">Hosting the user control on the webpage:</span></li>
</ul>
</h3>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<ol>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">Open the .aspx file in the design view on which you want to add the user control.</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">Drag and drop the user control (.ascx file). This will add the user control on to the webpage.</span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">You may now need to trap the event fired by the user control when the user click on the "Save" button. </span></li>
<li><span style="font-family: Times, 'Times New Roman', serif; text-align: -webkit-auto;">In C#, you need to first add a method to the page. This method should have the same signature as the event exposed by the user control. You then need to associate the event from the user control to the newly defined method. You can do this inside the page’s Init method. The following code shows an example.</span></li>
</ol>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<div class="page" style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;" title="Page 384">
<div class="section" style="background-color: #f3f3f3;">
<div class="layoutArea">
<pre><span style="font-family: Times, Times New Roman, serif;">protected void AddressUserControl1_SaveButtonClick(object sender, EventArgs e)
{
}
</span></pre>
<pre><span style="font-family: Times, Times New Roman, serif;">protected void Page_Init(object sender, EventArgs e)
{
</span></pre>
<pre><span style="font-family: Times, Times New Roman, serif;"> AddressUserControl1.SaveButtonClick +=
this.AddressUserControl1_SaveButtonClick;
</span></pre>
<span style="font-family: Times, Times New Roman, serif;">}</span></div>
</div>
</div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
<span style="font-family: Times, Times New Roman, serif; font-size: small;"><br /></span></div>
<div style="line-height: 15px; orphans: 2; text-align: -webkit-auto; widows: 2;">
</div>
</div>
</div>
<a href="http://www.codeproject.com/" rel="tag" style="display: none;">CodeProject</a></div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com2tag:blogger.com,1999:blog-7424628290310913618.post-52675502615358329782013-09-23T10:16:00.000-07:002013-09-23T10:16:39.985-07:00<div dir="ltr" style="text-align: left;" trbidi="on">
Very true....<br />
<h4 style="text-align: left;">
<span style="background-color: white; color: #37404e; font-family: 'lucida grande', tahoma, verdana, arial, sans-serif; font-size: 13px; line-height: 18px;">If five seconds of smile can make your photograph beautiful, just imagine how beautiful your life will be if you keep smiling always.... :):)</span></h4>
</div>
Komal Mangalhttp://www.blogger.com/profile/13261446370856175652noreply@blogger.com2