tag:blogger.com,1999:blog-700632859822641502024-02-08T08:43:48.349-08:00C Sharp Source CodesC Sharp Source CodesAnonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-70063285982264150.post-10775568861611153602017-02-28T11:01:00.002-08:002017-02-28T11:02:19.308-08:00How to create an XML file in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Data;
using System.Windows.Forms;
using System.Xml;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlTextWriter writer = new XmlTextWriter("product.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
writer.WriteStartElement("Table");
createNode("1", "Product 1", "1000", writer);
createNode("2", "Product 2", "2000", writer);
createNode("3", "Product 3", "3000", writer);
createNode("4", "Product 4", "4000", writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
MessageBox.Show("XML File created ! ");
}
private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer)
{
writer.WriteStartElement("Product");
writer.WriteStartElement("Product_id");
writer.WriteString(pID);
writer.WriteEndElement();
writer.WriteStartElement("Product_name");
writer.WriteString(pName);
writer.WriteEndElement();
writer.WriteStartElement("Product_price");
writer.WriteString(pPrice);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
}</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-77491871576277708932017-02-28T10:47:00.002-08:002017-02-28T10:47:37.916-08:00How to use C# Directory Class<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Directory.Exists("c:\\testDir1"))
{
//shows message if testdir1 exist
MessageBox.Show ("Directory 'testDir' Exist ");
}
else
{
//create the directory testDir1
Directory.CreateDirectory("c:\\testDir1");
MessageBox.Show("testDir1 created ! ");
//create the directory testDir2
Directory.CreateDirectory("c:\\testDir1\\testDir2");
MessageBox.Show("testDir2 created ! ");
//move the directory testDir2 as testDir in c:\
Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir");
MessageBox.Show("testDir2 moved ");
//delete the directory testDir1
Directory.Delete("c:\\testDir1");
MessageBox.Show("testDir1 deleted ");
}
}
}
}</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-19442910677233288402017-02-28T10:20:00.002-08:002017-02-28T10:20:41.782-08:00How to C# String Null<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str =null;
if (str == null)
{
MessageBox.Show("String is null");
}
else
{
MessageBox.Show("String is not null");
}
str = "test";
if (string.IsNullOrEmpty(str))
{
MessageBox.Show("String is empty or null");
}
else
{
MessageBox.Show("String is not empty or null");
}
}
}
}</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-23329325109099433132017-02-28T10:18:00.003-08:002017-02-28T10:18:51.674-08:00How to use C# string Copy<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "CSharp Copy() test";
str2 = string.Copy(str1);
MessageBox.Show(str2);
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run this C# source code you will get same content of str1 in str2 </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-20465743985490492172017-02-28T09:33:00.003-08:002017-02-28T09:33:43.731-08:00How to use C# string Equals<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = "Equals";
string str2 = "Equals";
if (string.Equals(str1, str2))
{
MessageBox.Show("Strings are Equal() ");
}
else
{
MessageBox.Show("Strings are not Equal() ");
}
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run this C# program you will get message "Strings are Equal() " </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-47061246268576164372017-02-28T09:16:00.000-08:002017-02-28T09:16:16.492-08:00How to use C# string Insert<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = "This is CSharp Test";
string insStr = "Insert ";
string strRes = str.Insert(15, insStr);
MessageBox.Show(strRes);
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you execute this C# program you will get the message box </pre>
<pre>showing "This is CSharp Insert Test" </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-80444107106344545952017-02-28T09:14:00.000-08:002017-02-28T09:14:03.609-08:00How to use C# string Contains<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "CSharp TOP 10 BOOKS";
if (str.Contains("TOP") == true)
{
MessageBox.Show("The string Contains() 'TOP' ");
}
else
{
MessageBox.Show("The String does not Contains() 'TOP'");
}
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run the C# program you will get the MessageBox with message </pre>
<pre>"The string Contains() 'TOP' " </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-6690718860221686392017-02-28T09:12:00.001-08:002017-02-28T09:12:27.874-08:00How to use C# string Compare<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "csharp";
str2 = "CSharp";
int result = 0;
result = string.Compare(str1, str2);
MessageBox.Show(result.ToString());
result = string.Compare(str1, str2, true);
MessageBox.Show(result.ToString());
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run this C# program first u will get -1 in message box and then 0</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-77845955651427786312017-02-28T09:11:00.000-08:002017-02-28T09:11:14.823-08:00How to use C# string IndexOf<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "CSharp TOP 10 BOOKS";
MessageBox.Show(str.IndexOf("BOOKS").ToString());
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you execute this C# program you will get the number 14 in the message box. </pre>
<pre>That means the substring "BOOKS" occurred and start in the position 14. </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-71888528134352928832017-02-27T11:16:00.002-08:002017-02-27T11:16:42.043-08:00How to validate a string in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool isNumeric;
int i;
string str = "100";
isNumeric = int.TryParse(str, out i);
MessageBox.Show("The value of i is " + i);
}
}
}</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-46289125356846658922017-02-27T11:15:00.000-08:002017-02-27T11:15:12.874-08:00How to use C# string Format<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double dNum = 0;
dNum = 32.123456789;
MessageBox.Show("Formated String " + string.Format("{0:n4}", dNum));
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run this C# source code you will get "Formated String 32.1234" </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-85976463893454722262017-02-27T11:12:00.002-08:002017-02-27T11:12:42.334-08:00How to use C# string Length<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "This is a Test";
MessageBox.Show(str.Length.ToString());
}
}
}</pre>
<pre style="color: maroon;">
</pre>
<pre style="color: maroon;">
</pre>
<pre>When you execute this C# source code you will get 14 in the messagebox. </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-33492961069431183502017-02-27T10:38:00.003-08:002017-02-28T09:17:27.338-08:00How to use C# string Clone<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
string str = "Clone() Test";
string clonedString = null;
clonedString = (String)str.Clone();
MessageBox.Show (clonedString);
}
}
}
}</pre>
<pre></pre>
<pre></pre>
<pre>When you run this C# program you will get the same content of the </pre>
<pre>first string "Clone() Test" </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-80254645558683389022017-02-27T10:36:00.000-08:002017-02-27T10:36:34.915-08:00How to use C# string Concat<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "Concat() ";
str2 = "Test";
MessageBox.Show(string.Concat(str1, str2));
}
}
}</pre>
<pre>
</pre>
<pre>
</pre>
<pre>When you run this C# source code you will get "Concat() Test " in message box. </pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-66932209098740804812017-02-27T10:34:00.000-08:002017-02-27T10:34:12.582-08:00How to use C# string Split<div dir="ltr" style="text-align: left;" trbidi="on">
<xmp>using System;
using System.Windows.Forms;</xmp><xmp>
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
string[] strArr = null;
int count = 0;
str = "CSharp split test";
char[] splitchar = { ' ' };
strArr = str.Split(splitchar);
for (count = 0; count <= strArr.Length - 1; count++)
{
MessageBox.Show(strArr[count]);
}
}
}
}</xmp></div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0tag:blogger.com,1999:blog-70063285982264150.post-89419570291824020512017-02-27T10:29:00.001-08:002017-02-27T10:29:31.647-08:00C Sharp Hello Word<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:csharp">using System;
namespace HelloWorld
{
class Hello
{
public static void Main(String[] args)
{
Console.WriteLine("Hello World");
}
}
}</pre>
</div>
Anonymoushttp://www.blogger.com/profile/07189420860542797980noreply@blogger.com0