Delete generic credential

Anandhan Sathyanarayanan 136 Reputation points


I'm using the below code to delete the generic credential from control panel credential manager (windows credentials), but it's not working.

Please validate the code and suggest solutions.

StartProcess("cmd.exe", new string[] { "/c cmdkey.exe", $@"/delete:{args[1]}" }, string.Empty);

public static void StartProcess(string exePath, string[] args, string vbscript)
                using (Process proc = new Process())
                    proc.StartInfo.Verb = "runas";
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.UseShellExecute = false;
                    proc.StartInfo.FileName = exePath;

                    if (args.Count() > 0 && string.IsNullOrWhiteSpace(vbscript) == false)
                        proc.StartInfo.Arguments = $@"{vbscript} \{string.Join(@"\ \", args)}\";
                    else if (args.Count() > 0)
                        proc.StartInfo.Arguments = $@"\{string.Join(@"\ \", args)}\";

                    proc.StartInfo.CreateNoWindow = true;
                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
                    proc.WaitForExit();//program running until your script exit

                    string standardOutput = proc.StandardOutput.ReadToEnd();
                    if (!string.IsNullOrWhiteSpace(standardOutput))
                        LoggingService.LogInfo("standardOutput: " + standardOutput);

                    string standardError = proc.StandardError.ReadToEnd();
                    if (!string.IsNullOrWhiteSpace(standardOutput))
                        LoggingService.LogError("standardError: " + standardError);

                    LoggingService.LogInfo("ExitCode: " + proc.ExitCode);
            catch (Exception ex)
                LoggingService.LogError(ex.Message + ex.StackTrace);
Microsoft Technologies based on the .NET software framework.
3,525 questions
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
324 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,483 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,136 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,576 Reputation points

    When I tested, I found that these two lines seemed to be enough:

                string deleteTarget = "testCredential";  
                Process.Start("cmdkey.exe", "/delete:"+deleteTarget);  

    By the way, if you need to view the list of credentials, you can refer to the following code:

                string cSplitString = "target=";   
                var targets = new List<string>();  
                var proc = new Process   
                    StartInfo = new ProcessStartInfo  
                        FileName = "cmdkey.exe",  
                        Arguments = "/list",  
                        UseShellExecute = false,  
                        RedirectStandardOutput = true,  
                        CreateNoWindow = true  
                while (!proc.StandardOutput.EndOfStream)  
                    string line = proc.StandardOutput.ReadLine();  
                    if (line.Contains(cSplitString))  
                        targets.Add(line.Substring(line.IndexOf(cSplitString) + cSplitString.Length));  

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

1 additional answer

Sort by: Most helpful
  1. Anandhan Sathyanarayanan 136 Reputation points

    Thanks for the update.. finally i investigated and found the issue. The issue due to the amendment of slash passed to the process start info.

    StartProcess("cmdkey.exe", new string[] { $@"/delete:{args[1]}" }, string.Empty);
    proc.StartInfo.Arguments = $@"{string.Join(@" ", args)}";