DriveInfo[] dinf = DriveInfo.GetDrives();
int i = 0;
while (i < dinf.Length)
{
MessageBox.show(dinf[i]);
}
Showing posts with label C# development. Show all posts
Showing posts with label C# development. Show all posts
Saturday, April 24, 2010
Listing Logical Drives
Saturday, April 17, 2010
Create XML file using C#
using System.Data;
using System.Drawing;
using System.Xml;using System.IO;
//codeXmlTextWriter xmlWriter = new XmlTextWriter("temp.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("root");
xmlWriter.Close();
// creation complete
doc = new XmlDocument();doc.Load("temp.xml");
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "firstnode", ""); // creating NodenewNode.InnerText = “Content”; // set your contentdoc.DocumentElement.AppendChild(newNode); //Adds a Node
doc.Save("temp.xml");
using System.Drawing;
using System.Xml;using System.IO;
//codeXmlTextWriter xmlWriter = new XmlTextWriter("temp.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("root");
xmlWriter.Close();
// creation complete
doc = new XmlDocument();doc.Load("temp.xml");
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "firstnode", ""); // creating NodenewNode.InnerText = “Content”; // set your contentdoc.DocumentElement.AppendChild(newNode); //Adds a Node
doc.Save("temp.xml");
Dynamically add Picture box using C# delegate
The below code is just abstract just to show simple use of delegate
using System.Data;
using System.IO;
using System.Drawing; using System.Text; using System.Threading; public partial class ImageView : Form{
Mydel a;
public delegate void Mydel(PictureBox p); public ImageView(){ InitializeComponent(); }
private void ImageView_Load(object sender, EventArgs e){
a = new Mydel(Add); Thread t = new Thread(load); t.Start(); }
public void load(){
DirectoryInfo dinfo = new DirectoryInfo(@"F:\Mypics"); FileInfo[] finfo = dinfo.GetFiles(); Graphics g = panel1.CreateGraphics(); int x = 20; int y = 20; for (int i = 0; i if (Image.FromFile(finfo[i].FullName) != null){
PictureBox p = new PictureBox(); p.Image = Image.FromFile(finfo[i].FullName);
p.SizeMode = PictureBoxSizeMode.StretchImage;
p.Left = x; p.Top = y;
p.ImageLocation = finfo[i].FullName;
p.Width = 140; p.Height = 90;
Object[] obj = new Object[1];
obj[0] = p;
Controls[Controls.IndexOf(panel1)].Invoke(a, obj); x += 150;
if (x > this.Width-100){
y += 100; x = 20; }
} if (i > 35) break;
}
}
public void Add(PictureBox p) {
panel1.Controls.Add(p);
}
}
}
using System.Data;
using System.IO;
using System.Drawing; using System.Text; using System.Threading; public partial class ImageView : Form{
Mydel a;
public delegate void Mydel(PictureBox p); public ImageView(){ InitializeComponent(); }
private void ImageView_Load(object sender, EventArgs e){
a = new Mydel(Add); Thread t = new Thread(load); t.Start(); }
public void load(){
DirectoryInfo dinfo = new DirectoryInfo(@"F:\Mypics"); FileInfo[] finfo = dinfo.GetFiles(); Graphics g = panel1.CreateGraphics(); int x = 20; int y = 20; for (int i = 0; i
PictureBox p = new PictureBox(); p.Image = Image.FromFile(finfo[i].FullName);
p.SizeMode = PictureBoxSizeMode.StretchImage;
p.Left = x; p.Top = y;
p.ImageLocation = finfo[i].FullName;
p.Width = 140; p.Height = 90;
Object[] obj = new Object[1];
obj[0] = p;
Controls[Controls.IndexOf(panel1)].Invoke(a, obj); x += 150;
if (x > this.Width-100){
y += 100; x = 20; }
} if (i > 35) break;
}
}
public void Add(PictureBox p) {
panel1.Controls.Add(p);
}
}
}
Function to retrieve RAM SIZE in MB C#
This is the sample code to retrieve total ram size of system using WMI (Windows Management Instrumentation) well it’s not perfect but it works
Name spaces
using System; using System.Management; using System.Collections.Generic;
using System.Text;
public int getRAMSize() {
int total=0;
try {
ConnectionOptions connection = new ConnectionOptions();
ManagementScope scope; scope = new ManagementScope("root\\CIMV2", connection); ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem"); ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get(){
total = Convert.ToInt32(queryObj["TotalPhysicalMemory"].ToString()); }
total = total / 1024; }
catch {} }
return total;
}
Name spaces
using System; using System.Management; using System.Collections.Generic;
using System.Text;
public int getRAMSize() {
int total=0;
try {
ConnectionOptions connection = new ConnectionOptions();
ManagementScope scope; scope = new ManagementScope("root\\CIMV2", connection); ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem"); ManagementObjectSearcher searcher =new ManagementObjectSearcher(scope, query);
foreach (ManagementObject queryObj in searcher.Get(){
total = Convert.ToInt32(queryObj["TotalPhysicalMemory"].ToString()); }
total = total / 1024; }
catch {} }
return total;
}
Subscribe to:
Posts (Atom)