Last active
August 29, 2023 16:54
-
-
Save oznotes/d9c7572ae1d157583d3c68ecdd473581 to your computer and use it in GitHub Desktop.
Add Folder to Defender Exclusion C# .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.ObjectModel; | |
using System.Management.Automation; | |
// Add Folder to Defender Exclusion running powershell script with-in C# .NET | |
namespace helper | |
{ | |
class Program | |
{ | |
// run the software with administrator rights | |
// app.manifest | |
// <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | |
static void Main() | |
{ | |
using (PowerShell PowerShellInst = PowerShell.Create()) | |
{ | |
// Get the Folder Path. | |
string path = Directory.GetCurrentDirectory(); | |
PowerShellInst.AddScript(@"Add-MpPreference -ExclusionPath '" + path + "'"); | |
Collection<PSObject> PSOutput = PowerShellInst.Invoke(); | |
Console.WriteLine(path+" to Windows Defender [added][+]"); | |
Console.Read(); | |
} | |
} | |
} | |
} |
Sorry to bother you again, is there a way to hide the cmd window during execution?
Try commenting out these two lines
//Console.WriteLine(path+" to Windows Defender [added][+]"); //Console.Read();
Thanks for your help, the black cmd window still appears for a short time, but it's much better.
must run as administrator !
Thanks for your help, the black cmd window still appears for a short time, but it's much better.
- Open the properties of your project by right-clicking on your project in the Solution Explorer and selecting 'Properties'.
- Go to the 'Application' tab.
- Change the 'Output type' dropdown to 'Windows Application'.
This will prevent a console window from showing up, but it will still allow your application to run.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try commenting out these two lines