TimeSpan timeSpan = new TimeSpan(DateTime.UtcNow.Ticks - scriptTime.Ticks);
string timeStr = timeSpan.Hours.ToString().PadLeft(2, '0') +
":" + timeSpan.Minutes.ToString().PadLeft(2, '0') +
":" + timeSpan.Seconds.ToString().PadLeft(2, '0');
September 22, 2010
Timespan Formatting
timeSpan.ToString("hh:mm:ss") equivalent. (Only available in .Net 4!)
July 29, 2010
StringCollection Class Extender
A helper class for the StringCollection class.
public static class StringCollectionExtender
{
// Convert a string collection to a multiline string where
// each entry in the collection becomes a line in
// multi-line string
public static string ToMultilineString(
this StringCollection sc)
{
StringBuilder res = new StringBuilder();
foreach (string str in sc)
{
res.AppendLine(str);
}
return res.ToString();
}
public static string[] ToStringArray(
this StringCollection sc)
{
string[] res = new string[sc.Count];
sc.CopyTo(res, 0);
return res;
}
// Convert a multiline string to a string collection with each line
// of the string becoming a new entry in the collection
public static StringCollection MultilineStringToStringCollection(
string str)
{
string[] lines = str.Split('\r');
StringCollection res = new StringCollection();
foreach (string line in lines)
{
res.Add(line.Trim('\n'));
}
return res;
}
}
July 28, 2010
Assembly Version Incrementor
Find and increment the assembly version info in an Assembly info file
public class AssemblyVersionIncrementor
{
public void IncrementAssemblyVersionString(FileSystemInfo fileInfo)
{
string[] lines = File.ReadAllLines(fileInfo.FullName);
lines = IncrementAssemblyVersionString(lines);
File.WriteAllLines(fileInfo.FullName, lines);
}
internal string[] IncrementAssemblyVersionString(string[] lines)
{
string[] res = new string[lines.Length];
int ix = 0;
foreach (string line in lines)
{
res[ix++] = IncrementAssemblyVersionString(line);
}
return res;
}
// Could be modified to find other types of Version Info by
// making the "AssemblyVersion" a parameter
internal string IncrementAssemblyVersionString(string line)
{
string res = line;
if (line.Contains("AssemblyVersion"))
{
string newLine = string.Empty;
string str = line;
// Check for comments
int commentStart = line.IndexOf("//");
// TODO /* ... */ style comments
if (commentStart >= 0)
{
str = line.Substring(0, commentStart);
}
if (str.Contains("AssemblyVersion"))
{
int start = str.IndexOf('"') + 1;
int end = str.IndexOf('"', start);
string assemblyVerStr = str.Substring(start, end - start);
Version av = new Version(assemblyVerStr);
Version newVer = av.IncrementRevision();
newLine = line.Substring(0, start) + newVer.ToString() +
line.Substring(end, line.Length - end);
}
res = newLine.Length > 0 ? newLine : line;
}
return res;
}
}
uses the following Version extension class:
internal static class VersionExtender
{
public static Version Increment(this Version version)
{
return Increment(version, 0x1L);
}
public static Version IncrementMajor(this Version version)
{
return Increment(version, 0x1000000000000L);
}
public static Version IncrementMinor(this Version version)
{
return Increment(version, 0x100000000L);
}
public static Version IncrementBuild(this Version version)
{
return Increment(version, 0x10000L);
}
public static Version IncrementRevision(this Version version)
{
return Increment(version, 0x1L);
}
// Good demo of using >> and << operators as well
private static Version Increment(this Version version, ulong inc)
{
ulong versnNum = (((ulong)version.Major & 0xFFFF) << 48) +
(((ulong)version.Minor & 0xFFFF) << 32) +
(((ulong)version.Build & 0xFFFF) << 16) +
((ulong)version.Revision & 0xFFFF);
versnNum += inc;
UInt16 major = (UInt16)(versnNum >> 48);
UInt16 minor = (UInt16)((versnNum >> 32) & 0xFFFF);
UInt16 build = (UInt16)((versnNum >> 16) & 0xFFFF);
UInt16 revision = (UInt16)(versnNum & 0xFFFF);
return new Version(major, minor, build, revision);
}
}
Labels:
AssemblyVersion,
Bit shift operators,
extension class,
Version
July 21, 2010
Quickly Add Xml To XmlDocument
This line loads an Xml document, looks for the presence of a particular line and if it is not there, adds it to the xml document. This is useful for updating a config file with a particular line of xml. Uses XPath to find the interesting element of the xml file.
public static void UpdateAnXmlNode(string xmlFileName)
{
XmlDocument xDoc = new XmlDocument();
try
{
// load the configuration file
xDoc.Load(xmlFileName);
// find the node of interest from the key
XmlNode theNode =
xDoc.SelectSingleNode(
@"/configuration/xxxConfiguration/moduleGroups/add[@trustName = 'Default']/modules");
bool customXmlLinePresent = false;
if (theNode != null)
{
customXmlLinePresent = theNode.InnerXml.Contains(
"XXX.YYY.Sandbox.DebugTree.Module");
}
string extraXml = string.Empty;
if (!customXmlLinePresent)
extraXml += @"<add moduleType=""XXX.YYY.Sandbox.DebugTree.Module, XXX.YYY.Sandbox"" />" + Environment.NewLine;
if (extraXml.Length > 0)
{
theNode.InnerXml += extraXml;
xDoc.PreserveWhitespace = true;
xDoc.Save(xmlFileName);
}
}
catch (XmlException ex)
{
DoSomethingWithError(ex);
}
}
July 16, 2010
FileSystemInfoExtender Class
Extend the FileSystemInfo class with enumerators to iterate through subdirectories, ditto with the DirectoryInfo class.
Now merged into this blog entry
Now merged into this blog entry
Labels:
DirectoryInfo,
extension class,
FileSystemInfo,
IEnumerable,
Iterators,
yield
FileAttributesExtender
Use Flags based enumerated types extender class to extend file attributes and give it Set/group theory like methods
public static class FileAttributesExtender
{
// Return lhs flags plus rhs flags
public static FileAttributes Union(this FileAttributes lhs, FileAttributes rhs)
{
return lhs | rhs;
}
// Return flags common to lhs and rhs
public static FileAttributes Intersection(this FileAttributes lhs, FileAttributes rhs)
{
return lhs & rhs;
}
// Return lhs flags minus rhs flags
public static FileAttributes Difference(this FileAttributes lhs, FileAttributes rhs)
{
FileAttributes common = Intersection(lhs, rhs);
int res = (int)lhs - (int)common;
return (FileAttributes)(res);
}
// Return true if lhs contains ALL the flags within rhs
public static bool Contains(this FileAttributes lhs, FileAttributes rhs)
{
FileAttributes common = Intersection(lhs, rhs);
return (common == rhs);
}
// Return true if lhs contains any of the flags within rhs
public static bool ContainsAnyOf(this FileAttributes lhs, FileAttributes rhs)
{
FileAttributes common = Intersection(lhs, rhs);
return ((int)common > 0);
}
// Return true if lhs contains none of the flags within rhs
public static bool ContainsNoneOf(this FileAttributes lhs, FileAttributes rhs)
{
FileAttributes common = Intersection(lhs, rhs);
return ((int)common == 0);
}
// NON-extension methods here
public static FileAttributes FromString(string source)
{
FileAttributes res = (FileAttributes)Enum.Parse(typeof(FileAttributes), source, true);
return res;
}
}
Subscribe to:
Posts (Atom)