Posts Tagged ‘C#’
How to open the windows explorer programmatically in .net
How to open the windows explorer using vb.net
-
System.Diagnostics.Process.Start("explorer.exe", "c:\tmp")
Extension method in .net
Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.
Example if we want to extend the functionality of the String type
-
Imports System.Runtime.CompilerServices
-
-
Module StringExtensions
-
-
<Extension()>
-
Public Sub Print(ByVal aString As String)
-
Console.WriteLine(aString)
-
End Sub
-
-
<Extension()>
-
Public Sub PrintAndPunctuate(ByVal aString As String,
-
ByVal punc As String)
-
Console.WriteLine(aString & punc)
-
End Sub
-
-
End Module
To use that function
-
Sub Main()
-
-
Dim example As String = "Example string"
-
example.Print()
-
-
example = "Hello"
-
example.PrintAndPunctuate(".")
-
example.PrintAndPunctuate("!!!!")
-
-
End Sub
C#.NET
If you notice the parameter there is a this keyword, that is how it done.
-
namespace ExtensionMethods
-
{
-
public static class StringExtensions
-
{
-
public static int Print(<strong>this </strong>String str)
-
{
-
Console.WriteLine(String );
-
}
-
}
-
}
-
-
string s = "Hello Extension Method";
-
s.Print();
The install location for prerequisites has not been set to
Posted by: admin in .NET, Deployment on June 9th, 2010
Follow this steps to solve this problem:
Update the Package Data
- Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
- Edit the Product.xml file in Notepad.
- Paste the following into the < PackageFiles > element:
- Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-x86-en-US.exe” and change the PublicKey value to:
3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B79
4C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163
F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE
361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E
817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C13
78D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412
FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B2842
3C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D208557
6810203010001 - Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-amd64-en-US.exe” and change the PublicKey value to the same as in step 4 above
- Save the product.xml file
Download and Extract the Core Installation Files
- Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
- Download the dotNetFx35.exe file to your local disk.
- Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
- At the command prompt, type:
dotNetFx35.exe /x:.
This will extract the Framework files to a folder named “WCU” in the current directory.
Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems).
Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following: DotNetFx35SP1 (folder)
-
- dotNetFX20 (folder
- dotNetFX30 (folder)
- dotNetFX35 (folder)
- dotNetMSP (folder)
- TOOLS folder)
- en (or some other localized folder)
- dotNetFx35setup.exe (file)
You may now delete the files and folders you downloaded and extracted in steps 2 and 4.
You can read more on the attached readme file.
How to remove parent node checkbox of the Treeview
Add this script to your common module
VB.NET
-
-
Public Const TVIF_STATE As Integer = &H8
-
Public Const TVIS_STATEIMAGEMASK As Integer = &HF000
-
Public Const TV_FIRST As Integer = &H1100
-
Public Const TVM_SETITEM As Integer = TV_FIRST + 63
-
-
Public Structure TVITEM
-
Public mask As Integer
-
Public hItem As IntPtr
-
Public state As Integer
-
Public stateMask As Integer
-
Public lpszText As String
-
Public cchTextMax As Integer
-
Public iImage As Integer
-
Public iSelectedImage As Integer
-
Public cChildren As Integer
-
Public lParam As IntPtr
-
End Structure
-
-
Public Sub RemoveCheckbox(ByVal nod As SiteTreeNode)
-
Dim tvi As TVITEM
-
tvi.hItem = nod.Handle
-
tvi.mask = TVIF_STATE
-
tvi.stateMask = TVIS_STATEIMAGEMASK
-
tvi.state = nod.Index << 12
-
SendMessage(nod.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi)
-
End Sub
Then call the subroutine
Public Sub TreeNode_SetStateImageIndex(ByVal node As TreeNode, ByVal index
As Integer)
RemoveCheckbox(node)
End Sub
C#
-
-
using System.Runtime.InteropServices;
-
-
namespace Treeview___CheckBoxes
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
// Iterate over the root nodes, removing their checkboxes
-
for (int n = 0; n<treeView1.Nodes.Count; n++)
-
{
-
TreeNode node = treeView1.Nodes[n];
-
TVITEM tvItem = new TVITEM();
-
tvItem.hItem = node.Handle;
-
tvItem.mask = TVIF_STATE;
-
tvItem.stateMask = TVIS_STATEIMAGEMASK;
-
tvItem.state = 0;
-
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
-
Marshal.StructureToPtr(tvItem, lparam, false);
-
SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
-
}
-
}
-
public const int TVIF_STATE = 0×8;
-
public const int TVIS_STATEIMAGEMASK = 0xF000;
-
public const int TV_FIRST = 0×1100;
-
public const int TVM_SETITEM = TV_FIRST + 63;
-
public struct TVITEM
-
{
-
public int mask;
-
public IntPtr hItem;
-
public int state;
-
public int stateMask;
-
[MarshalAs(UnmanagedType.LPTStr)]
-
public String lpszText;
-
public int cchTextMax;
-
public int iImage;
-
public int iSelectedImage;
-
public int cChildren;
-
public IntPtr lParam;
-
}
-
[DllImport(“user32.dll”)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
-
-
}
-
}
String to Numeric Conversion
Data Conversion in c# .net
| Numeric Type | Method |
|---|---|
| decimal | Convert.ToDecimal(String) |
| float | Convert.ToSingle(String) |
| double | Convert.ToDouble(String) |
| short | Convert.ToInt16(String) |
| long | Convert.ToInt64(String) |
| ushort | Convert.ToUInt16(String) |
| uint | Convert.ToUInt32(String) |
| ulong | Convert.ToUInt64(String) |
How to: Convert a string to an int (C# Programming Guide)
@ Symbol in C##
The @ symbol tells the string constructor to ignore escape characters and line break
The following two strings are therefore identical:
-
-
string p1 = "\\\\My Documents\\My Files\\";
-
string p2 = @"\\My Documents\My Files\";