using System.Security.Principal; ... string userName = Environment.MachineName + @"\Administrator"; NTAccount account= new NTAccount(userName); SecurityIdentifier sid = (SecurityIdentifier)
account.Translate(typeof(SecurityIdentifier)); string machineSid = sid.AccountDomainSid.ToString();
November 30, 2006
Extracting the MachineSid
Using an ErrorProvider
1. Add it to the form
// 2. Initialise it: errorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink; errorProvider.SetIconAlignment(this, ErrorIconAlignment.MiddleRight); errorProvider.SetIconPadding(this, 2); // 3. To flag an error errorProvider.SetError(tbName, "The specified user name is unknown"); // 4. To stop it flashing errorProvider.Clear();
Useful Assembly Methods
using System.Reflection; //To retrieve the version number from the // assembly in your code, you use can do this: string strVersion =
Assembly.GetExecutingAssembly().GetName().Version.ToString(); //To retrieve the version number from the // assembly that is calling your assembly, // you can use this: string strVersion =
Assembly.GetCallingAssembly().GetName().Version.ToString(); // Retrieving the executing assembly's path as a Uri Uri uri = new Uri(System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase)); // and as a string path string path = new Uri(System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase)).LocalPath;
Performing FileToString and StringToFile.
The required methods are on the File class:
using System.IO; string File.ReadAllText (string path, Encoding enc); string File.ReadAllText (string path); void File.WriteAllText (string path, string contents, Encoding encoding); void File.WriteAllText (string path, string contents);
Reading and Writing Xml
#region Read/Write Xml
using System.Text;
using System.Xml;
using System.IO;
...
// Reading from XML Reader
private void FromXml(XmlReader reader)
{
bool corrupt = false;
while (!reader.EOF && !corrupt)
{
if (reader.NodeType != XmlNodeType.EndElement)
{
switch (reader.Name)
{
case "BaseElement":
string tmp = reader.GetAttribute("Guid");
m_Guid = new Guid(tmp);
break;
case "EnumType":
string tmp = reader.ReadElementContentAsString();
m_EnumType = (EnumType)Enum.Parse(
typeof(EnumType), tmp);
break;
case "Id":
m_Id = reader.ReadElementContentAsString();
break;
case "Name":
m_SomeThing = reader.ReadElementContentAsString();
break;
case "SomeDate":
string value = reader.ReadElementContentAsString();
DateTime m_SomeDate;
DateTime.TryParse(value, out m_SomeDate);
break;
default:
break;
}
}
try
{
reader.Read();
}
catch (XmlException)
{
corrupt = true;
}
}
}
// Reading from XML String
private void FromXmlString(string xml)
{
XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Element,
new XmlParserContext(null, new XmlNamespaceManager(
new NameTable()), null, XmlSpace.Default));
try
{
reader.MoveToContent();
FromXml(reader);
}
finally
{
reader.Close();
}
}
// Reading from XMLFile
public void FromXmlFile(string xmlFile)
{
if (File.Exists(xmlFile))
{
XmlTextReader reader = new XmlTextReader(xmlFile);
try
{
reader.MoveToContent();
FromXml(reader);
}
finally
{
reader.Close();
}
}
}
// Writing to XML Writer
public void ToXml(XmlWriter writer)
{
//m_Writer.WriteDocType("", null, null, null);
writer.WriteStartElement("BaseElement");
writer.WriteAttributeString("Guid", m_Guid.ToString());
WriteContent(writer);
writer.WriteEndElement();
writer.Flush();
}
protected virtual void WriteContent(XmlWriter writer)
{
writer.WriteElementString("EnumType", m_EnumType.ToString());
writer.WriteElementString("Id", m_Id);
writer.WriteElementString("Name", m_Name);
writer.WriteElementString("Date", DateTime.Now.ToUniversalTime().ToString());
}
// Writing to XML String
public override string ToXmlString()
{
StringWriter sw = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(sw);
try
{
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
ToXml(writer);
writer.WriteEndDocument();
}
finally
{
writer.Close();
}
return sw.GetStringBuilder().ToString();
}
public void ToXmlFile(string xmlFile, Encoding encoding)
{
XmlTextWriter writer = new XmlTextWriter(xmlFile, encoding);
try
{
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
ToXml(writer);
writer.WriteEndDocument();
}
finally
{
writer.Close();
}
}
#endregion Read/Write Xml
November 29, 2006
Basic OnPaint and OnSizeChanged Methods for a Resizable Control
// after InitializeComponent()
this.ResizeRedraw = true; // If you want resizing to cause a redraw
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
try
{
pea.Graphics.FillRectangle(Brushes.DarkOliveGreen,
new Rectangle(0, 0, Width, Height));
// Create a new pen.
using (Pen pen = new Pen(Brushes.Black))
{
// Draw a line
Point pt1 = new Point(ClientRectangle.Left,
ClientRectangle.Bottom - 1);
Point pt2 = new Point(ClientRectangle.Right,
ClientRectangle.Bottom - 1);
pea.Graphics.DrawLine(pen, pt1, pt2);
}
}
catch (Exception ex)
{
//TODO: Log these errors.
}
}
Reading the Registry in c#
private static void GetMachineGuid()
{
string machineGuid = string.Empty;
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Cryptography");
machineGuid = (string)rk.GetValue(@"MachineGuid");
Console.WriteLine("MachineGuid: " + machineGuid);
}
Subscribe to:
Comments (Atom)