In my PSCmdlet
class I'm asking the user to make a choice with PromptForChoice
. The user is prompted to select the connectionstring he wants to use. I can't manage to give the ChoiceDescriptions
the right format.
choiceDescriptions.Add(new ChoiceDescription("&"+i+externalConfig.ConnectionStrings.ConnectionStrings[i].ConnectionString));
Gives me this
Connection String
Choose the connection string you want to use
[0] 0data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
[1] 1Valid Connection String;
[2] 2Data Source=111.111.1.1;Initial Catalog=Test;User id=User;Password=Password;MultipleActiveResultSets=true
[?] Help (default is "0"):
As you can see the numbers are also in the connectionstring texts, which is not what I want.
This is what I'm trying to achieve:
[0] data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
etc. etc.
As result I need the choice of the user as an int
. How can I get the result I want to achieve?
Code:
[Cmdlet(VerbsCommon.Get, "DemoNames")]
public class Get_DemoNames : PSCmdlet
{
[Parameter(Position = 0, Mandatory = false)]
public string prefix;
System.Diagnostics.Debugger.Launch();
var environment = Package.GetGlobalService(typeof(DTE)) as DTE2;
if (environment == null)
{
throw new Exception("Need a DTE");
}
var solution = new DirectoryInfo(environment.Solution.FullName);
var directory = string.Empty;
if (solution.Parent != null)
{
directory = Path.Combine(solution.Parent.FullName, @"Datamodel\bin\Debug");
}
else
{
throw new Exception("No directory for DLL-files");
}
var whereBuilder = new WhereImporter(directory);
EfXmlDependencyInjectionContainer.Wheres = whereBuilder.AllEfXmlWheres();
// get propertyinfo from database-contexts
var contextBuilder = new EfXmlContextPropertyBuilder(directory);
EfXmlDependencyInjectionContainer.ContextProperties = contextBuilder.GetPropertyInfo();
//get datamodels
var datamodelBuilder = new EfXmlDatamodelBuilder(directory);
/* Get the app/web config file */
var configFile = string.Empty;
var projects = environment.Solution.Projects.DTE.ActiveSolutionProjects;
foreach (var project in projects)
{
foreach (EnvDTE.ProjectItem item in project.ProjectItems)
{
if (Regex.IsMatch(item.Name, "(app|web).config", RegexOptions.IgnoreCase))
{
configFile = item.FileNames[0];
break;
}
}
}
/* Open config file */
var configMap = new ExeConfigurationFileMap
{
ExeConfigFilename = configFile
};
var externalConfig = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configMap,
ConfigurationUserLevel.None);
var conString = string.Empty;
/* No connectionstrings are found */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count == 0)
{
throw new Exception($"No connectionstrings found in: {configFile}");
}
/* If there's only one connectionstring, pick that one. */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count == 1)
{
conString = externalConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
}
/* Give the user the choice to choose the connectionstring. */
if (externalConfig.ConnectionStrings.ConnectionStrings.Count > 1)
{
var choiceDescriptions = new Collection<ChoiceDescription>();
for (int i = 0; i < externalConfig.ConnectionStrings.ConnectionStrings.Count; i++)
{
choiceDescriptions.Add(new ChoiceDescription("&"+i+externalConfig.ConnectionStrings.ConnectionStrings[i].ConnectionString));
}
var choice = this.Host.UI.PromptForChoice("Connection String", "Choose the connection string you want to use",
choiceDescriptions, 0);
conString = externalConfig.ConnectionStrings.ConnectionStrings[choice].ConnectionString;
WriteObject(choice);
}
var parser = new Parser
{
CurrentProjectDirectory = solution.Parent.FullName,
DbConnectionString = conString
};
parser.UpdateXml(datamodelBuilder);
}
}