How to delete .svn files and folders using Windows Batch Script

to delete .svn files and folders using Windows Batch Script you can use the following snippet.

Sample Windows Batch Script

FOR /R "C:\Users\CodeSnippets.Fesslersoft.de\Documents\Visual Studio 2013\Projects\ConsoleApplication" %%X IN (.svn) DO (RD /S /Q "%%X" 2>nul)

Replace C:\Users\CodeSnippets.Fesslersoft.de\Documents\Visual Studio 2013\Projects\ConsoleApplication with your Solution’s Path.

Please rate this snippet

How to check if a Form is Open in C# and VB.NET

To Check if a Form is open in C# and VB.NET you can use the snippet below.

Sample C#

public static bool FormIsOpen(FormCollection application, Type formType)
{
	//usage sample: FormIsOpen(Application.OpenForms,typeof(Form2)
	return Application.OpenForms.Cast<Form>().Any(openForm => openForm.GetType() == formType);
}

Sample VB.NET

Public Shared Function FormIsOpen(application__1 As FormCollection, formType As Type) As Boolean
	'usage sample: FormIsOpen(Application.OpenForms,GetType(Form2)
	Return Application.OpenForms.Cast(Of Form)().Any(Function(openForm) openForm.[GetType]() = formType)
End Function

Please rate this snippet

How to Log off, Restart or Shutdown Windows using Batch

To Logoff, Restart or Shutdown Windows using Batch you can Simply execute shutdown with the needed parameters. To execute the shutdown command you need the sufficient user privileges.

List of Parameters

  • -l Logs off the current user, this is also the default. -m ComputerName takes precedence.
  • -s Shuts down the local computer.
  • -r Reboots after shutdown.
  • -a Aborts shutdown. Ignores other parameters, except -l and ComputerName. You can only use -a during the time-out period.
  • -f Forces running applications to close. Note that if you are on a remote machine without using -f your session maybe closed but a hanging program blocks the Shutdown
  • -m [ \\ ComputerName ] Specifies the computer that you want to shut down.
  • -t xx Sets the timer for system shutdown in xx seconds. The default is 20 seconds.
  • -c ” message “ Specifies a message to be displayed in the Message area of the System Shutdown window. You can use a maximum of 127 characters. You must enclose the message in quotation marks.
  • -d [ u ][ p ] : xx : yy Lists the reason code for the shutdown. Reason Codes (u=Indicates a user code), (p=Indicates a planned shutdown cod), (xx=Specifies the major reason code (0-255)), (yy=Specifies the minor reason code (0-65536)
  • /? Displays help at the command prompt.
  • FOR MORE INFORMATIONS AND DETAILS TAKE A LOOK AT THE MSDN.

    Please rate this snippet

    How to get Windows Product Name in C# and VB.NET

    To get Windows Product Name in C# and VB.NET you can use the following snippet.

    Sample C#

    public static string GetWindowsProdutName()
    {
    	var name = (new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>().Select(x => x.GetPropertyValue("Caption"))).First();
    	return name != null ? name.ToString() : "Unknown";
    }
    

    Sample VB.NET

    Public Shared Function GetWindowsProdutName() As String
    	Dim name = (New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").[Get]().OfType(Of ManagementObject)().[Select](Function(x) x.GetPropertyValue("Caption"))).First()
    	Return If(name IsNot Nothing, name.ToString(), "Unknown")
    End Function
    

    Tipp: To use ManagementObjectSearcher you need to set a Reference to System.Management.

    Please rate this snippet

    How to Remove Illegal Filename Characters in C# and VB.NET

    To Remove Illegal Filename Characters in C# and VB.NET you can use the following snippet.

    Sample C#

    public static string RemoveIllegalFileNameChars(string input, string replacement="")
    {
    	var regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
    	var r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
    	return r.Replace(input, replacement);
    }
    

    Sample VB.NET

    Public Shared Function RemoveIllegalFileNameChars(input As String, Optional replacement As String = "") As String
    	Dim regexSearch = New String(Path.GetInvalidFileNameChars()) & New String(Path.GetInvalidPathChars())
    	Dim r = New Regex(String.Format("[{0}]", Regex.Escape(regexSearch)))
    	Return r.Replace(input, replacement)
    End Function
    

    Please rate this snippet

    How to hide a Console Window in C# and VB.NET

    To hide a Console Window in C# and VB.NET you can use the following snippet.

    Sample C#

    public static class Consoles
    {
    	[DllImport("user32.dll")]
    	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    	[DllImport("user32.dll")]
    	public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    	public static void SetConsoleWindowVisibility(bool visible, IntPtr hWnd)
    	{
    		ShowWindow(hWnd, !visible ? 0 : 1);
    	}
    }
    

    usage:

    var hWnd = Consoles.FindWindow(null, Console.Title);
    Consoles.SetConsoleWindowVisibility(false, hWnd);
    

    Sample VB.NET

    Public NotInheritable Class Consoles
    	Private Sub New()
    	End Sub
    	<DllImport("user32.dll")> _
    	Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
    	End Function
    
    	<DllImport("user32.dll")> _
    	Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
    	End Function
    
    	Public Shared Sub SetConsoleWindowVisibility(visible As Boolean, hWnd As IntPtr)
    		ShowWindow(hWnd, If(Not visible, 0, 1))
    	End Sub
    End Class
    

    usage:

    Dim hWnd = Consoles.FindWindow(Nothing, Console.Title)
    Consoles.SetConsoleWindowVisibility(False, hWnd)
    

    Please rate this snippet

    How to reset the windows audio mixer in Batch

    To reset the windows audio mixer in Batch you can use the following snippet.
    This snippet will set all volume levels equal. If you know a simpler way to reset the audio mixer, without setting all volume levels manually please let us know.

    Sample Batch

    @ECHO OFF
    ECHO Resetting Volume Mixer Settings!
    NET STOP Audiosrv
    NET STOP AudioEndpointBuilder
    REG DELETE "HKCU\Software\Microsoft\Internet Explorer\LowRegistry\Audio\PolicyConfig\PropertyStore" /F
    REG ADD "HKCU\Software\Microsoft\Internet Explorer\LowRegistry\Audio\PolicyConfig\PropertyStore"
    NET START Audiosrv
    

    Please rate this snippet

    How to list all installed windows services in C# and VB.NET

    To list installed services in C# and VB.NET you can use the following snippet.

    Sample C#

    public static void ListInstalledServices()
    {
    	var services = ServiceController.GetServices();
    	foreach (var service in services)
    	{
    		Console.WriteLine(@"Installed: {0}", service.ServiceName);
    	}
    }
    

    Sample VB.NET

    Public Shared Sub ListInstalledServices()
    	Dim services = ServiceController.GetServices()
    	For Each service As var In services
    		Console.WriteLine("Installed: {0}", service.ServiceName)
    	Next
    End Sub
    

    Please rate this snippet

    How to delete a file or folder in Powershell

    To delete a file or Folder in Powershell you can use the following snippet.

    Sample Powershell

    if you want to delete a file use

    remove-item -path "C:\Users\Codesnippets\Desktop\Test.xml" -force -recurse
    

    if you want to delete a folder use

    remove-item -path "C:\Users\Codesnippets\Desktop\Folder1" -force -recurse
    

    for more informations see Using the Remove-Item Cmdlet

    Please rate this snippet