Friday, August 30, 2013

CSharp C# Regular Expression RegExp for SEARCH and REPLACE with examples Notepad++ and Visual Studio

CSharp C# Regular Expression RegExp for SEARCH and REPLACE with examples Notepad++ and Visual Studio




Base CLK example using Notepad++ (note that it will not work if file is not saved...):


Console.WriteLine("Stuff n more stuff");
Search:  (Console.?WriteLine\()(\").*(\"\);)
replace: \1\2dog\3



Example:  Find Console.WriteLine items with "," and {} references.  Replace Console.WriteLine with LogInforAndConsole and surround full text, comma, and value in string or variable with the String.Format function.  And ignore non-comma signatures.
Before:
Console.WriteLine("Stuff n more stuff={0}", "something else");
Console.WriteLine("Hello all!");
Console.WriteLine("Stuff n more stuff={0}", myVar);


Search:  (Console.?WriteLine\()(\")(.*,.*)(\);)
                       \1                      \2   \3  \4
replace: LogInfoAndConsole\(string.Format\(\2\3\)\4
replace: LogInfoAndConsole(string.Format($2$3)$4    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole(string.Format("Stuff n more stuff={0}", "something else"));
Console.WriteLine("Hello all!");
LogInfoAndConsole(string.Format("Stuff n more stuff={0}", myVar));


Example:  Find all simple Console.Writeline items and replace Console.WriteLine with LogInforAndConsole.
Before:
 Console.WriteLine("Hello all!");


Search:  (Console.?WriteLine\()(\")(.*)(\);)
                       \1                      \2   \3  \4
replace: LogInfoAndConsole\(\2\3\4
replace: LogInfoAndConsole(s$2$3$4    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole("Hello all!");


Example: Find all Console.WriteLine with variable only values and substitute Console.WriteLine with LogInfoAndConsole.
Before:
Console.WriteLine(verificationErrors.ToString());
Console.WriteLine(myVar);


Search:  (Console.?WriteLine\()(.*)(\);)
                       \1                        \2   \3
replace: LogInfoAndConsole\(\2\3
replace: LogInfoAndConsole($2$3    ($ instead of \# and no slash(escape) in front of parenthesis with C# and Visual Studio search)


After:
LogInfoAndConsole(verificationErrors.ToString());
LogInfoAndConsole(myVar);

Example of Notepad++ UI:






Friday, August 23, 2013

CSharp C# string to enum example

CSharp C# string to enum example


Output:

enumNow int value=1, wordvalue=fred
enumNow int value=0, wordvalue=joe



Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.IO;

class Stuff4
{
    static void Main()
    {
        ShowEnumInfoOnPassedMatchingStringToEnum("fred");
        ShowEnumInfoOnPassedMatchingStringToEnum("joe");
        Console.ReadLine();
    }

    private static void ShowEnumInfoOnPassedMatchingStringToEnum(string enumWord)
    {
        ProcessToRun enumNow = (ProcessToRun)Enum.Parse(typeof(ProcessToRun), enumWord);
        Console.WriteLine("enumNow int value={0}, wordvalue={1}", (int)enumNow, enumNow);       
    }

        public enum ProcessToRun
        {
            joe,
            fred
        }
}


CSharp C# XDocument Very Simple Read and locate with Linq

CSharp C# XDocument Very Simple Read and locate with Linq


The XML Target:

<?xml version="1.0" encoding="utf-8"?><root>
<TestEnvironment>QA_TestZoneNow</TestEnvironment>
<Browser>Chrome</Browser>
<NewCompanyNameForTrackerAdminRemoteSetup>CLK_Q2_007</NewCompanyNameForTrackerAdminRemoteSetup>
<CreateAndUseNewCompany>TRUE</CreateAndUseNewCompany>
</root>


The output:

Iterate all the elements by their name and value properties:
elementNow=TestEnvironment
  ValueNow=QA_TestZoneNow
elementNow=Browser
  ValueNow=Chrome
elementNow=NewCompanyNameForTrackerAdminRemoteSetup
  ValueNow=CLK_Q2_007
elementNow=CreateAndUseNewCompany
  ValueNow=TRUE
Find the "Browser" tag in full:
browserNow element and value together=<Browser>Chrome</Browser>
Browser tag name=Browser and tag value=Chrome


Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Stuff4
{
    static void Main()
    {
        string targetFile = @"C:\Dump2\UnattendedTestThreadAppConfigUpdates.xml";
        XDocument fileNow = XDocument.Load(targetFile);
        IEnumerable<XElement> listOfElements = fileNow.Descendants().Elements();
        Console.WriteLine("Iterate all the elements by their name and value properties:");
        foreach (XElement elementNow in listOfElements)
        {
            Console.WriteLine("elementNow={0}", elementNow.Name);
            Console.WriteLine("  ValueNow={0}", elementNow.Value);
        }
        Console.WriteLine("Find the \"Browser\" tag in full:");
       XElement browserNow = listOfElements.First((eNow) => eNow.Name == "Browser");
       Console.WriteLine("browserNow element and value together={0}", browserNow);
       Console.WriteLine("Browser tag name={0} and tag value={1}", browserNow.Name, browserNow.Value);
        Console.ReadLine();
    }
}


Tuesday, August 20, 2013

CMD DOS how to delete erase an environmental variable

CMD DOS how to delete erase an environmental variable

set theVariable=

This will remove it from the environment variables

C:\>set aVar=fred and barney

C:\>echo %aVar%
fred and barney

C:\>set aVar=

C:\>echo %aVar%
%aVar%

C:\>


Note:  to show set variables:  set ""