Dan H
Dan H

Reputation: 3

VB.net storing variable names in sql for use in process launch arguments

I'm building a diagnostic tool with a script and application launcher, and I'm doing this in .net 3.5. We may be able to use .net 4 soon but nothing above that because of windows xp!

The menu's are being populated from a sql table, linking to these applications.

Some of these applications are scripts and require arguments to run, I don't want people to have to type anything in, and the application will store these argument attributes such as user and hostname as part of the diagnostic tool. If the arguments required for this script are stored on sql as a string, how might I convert that to the variable value?

for example:

Public strHostname As String = "pc1" 'global variable set by computer

strArguments = "/c /v:strHostname /wait" 'pulled from sql

when building my arguments string for process.start, i want to switch strHostname from strArguments with "pc1" from my public variable.

Obviously this would be best done with a standard string find and replace, but I have literally hundreds of variables and do not wish to construct a case statement that big.

Is there any other way I could take the string "strHostname" and use that to find the value of the public strHostname?

Upvotes: 0

Views: 65

Answers (1)

Polyfun
Polyfun

Reputation: 9639

Create a Parameter class like this:

class Parameter
{
    public string Name { get; set; }
    public string Value { get; set; }
}

On the database you have a Parameter table with Name and Value columns (nvarchar in SQL Server), and the table is populated something like this (I only show the first row):

Name           Value
%HostName      pc1

I prefix the parameter name with a '%' to make it easy to find it in the arguments string.

Read the Parameter table from the database into e.g., an array/list of Parameters, and then use the Parameters to replace all parameters in the arguments:

string arguments = "/c /v:%HostName /wait";
foreach (Parameter parameter in Parameters)
{
    arguments = arguments.Replace(parameter.Name, parameter.Value);
}

Upvotes: 1

Related Questions