What's the right syntax to get a BcdObject using ManagementObject? for single parameters I use:
var obj = new ManagementObject(#"root\WMI", string.Format("BcdObject.Id = '{0}'"), null);
But I'm not sure how to add additional parameters (is it AND, or ,, or something else?), something like:
var bcdObj = new ManagementObject(#"root\WMI",
string.Format("BcdObject.Id = '{0}' AND BcdObject.StoreFilePath = '{1}'",
"{current}", ""),
null);
This should be the way to go:
var bcdId = "{current}";
var sfp = "";
var obj = new ManagementObject(
"root\\WMI:BcdObject.Id=\"" + bcdId + "\",StoreFilePath=\"" + sfp + "\"");
Note that even if you merely put a space after the comma it won't work. Good luck!
You can pass the filter string as a second parameter in the constructor (like in your original code) but same rules apply - no spaces.
Related
I use the following function:
HttpUtility.ParseQueryString
I'm able to extract one parameter value so far. However now I'd like if possible to obtain all parameters in an array, and another array with all their value. Is there a way to do this?
For one parameter value I do this:
HttpUtility.ParseQueryString(myUri.Query).Get(param)
In the worst case, if I can't have an array for both, if I could get a string array for the parameters, I could then use the line above with every parameters to get the value.
You can get dictionary of parameters and then get each parameter value.
string uri = "http://www.example.com?param1=good¶m2=bad";
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility
.ParseQueryString(queryString);
var paramsList = new Dictionary<string, string>();
foreach (var parameter in queryDictionary)
{
var key = (string) parameter;
var value = queryDictionary.Get(key);
paramsList.Add(key, value);
}
Taken from documentation:
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
StringBuilder sb = new StringBuilder("<br />");
foreach (String s in qscoll.AllKeys)
{
sb.Append(s + " - " + qscoll[s] + "<br />");
}
ParseOutput.Text = sb.ToString();
So the result of ParseQueryString is a Dictionary, which holds all parsed data, just iterate through it :)
What is the best way to format the below string in a way so that I can separate out and find the value of PractitionerId, PhysicianNPI, PhysicianName etc.
"PractitionerId:4343343434 , PhysicianNPI: 43434343434, PhysicianName:
John, Doe, PhysicianPhone:2222222222 , PhysicianFax:3333333333 "
So finally I want something like this:
var practitionerId = "4343343434 ";
var physNPI = "43434343434";
var phyName = "John, Doe";
I was thinking of splitting with the names and finding the values assigned to each field but I am not sure if that is the best solution to it.
You could probably generalise this with a regular expression, then use it to build a dictionary/lookup of the terms.
So:
var input= "PractitionerId:4343343434 , PhysicianNPI: 43434343434,"
+ " PhysicianName: John, Doe, PhysicianPhone:2222222222 ,"
+ " PhysicianFax:3333333333";
var pattern = #"(?<=(?<n>\w+)\:)\s*(?<v>.*?)\s*((,\s*\w+\:)|$)";
var dic = Regex
.Matches(input, pattern)
.Cast<Match>()
.ToDictionary(m => m.Groups["n"].Value,
m => m.Groups["v"].Value);
So now you can:
var practitionerId = dic["PractitionerId"];
or
var physicianName = dic["PhysicianName"];
You could get the exact information, doing something like:
var str = "PractitionerId:4343343434 , PhysicianNPI: 43434343434, PhysicianName: John, Doe, PhysicianPhone:2222222222 , PhysicianFax:3333333333 ";
var newStr = str.Split(',');
var practitionerID = newStr[0].Split(':')[1]; // "4343343434"
var physicianNPI = newStr[1].Split(':')[1].Trim(); // "43434343434"
var phyName = newStr[2].Split(':')[1].Trim() + "," + newStr[3]; // "John, Doe"
There are cleaner solutions using Regex patterns though.
Also, you need to parse the corresponding variables to the specific data type you want. Everything here is being treated as a string
Since you seperate information with ",", this should work:
string[] information = yourWholeString.Split(",");
string practitionerId = information[0];
string physNPI = information[1];
string phyName = information[2] + information[3];
I've passed a really long Query String from one page to another in my Windows Phone 8 project.
I need to pass these parameters from the new page to another page but don't want to reconstruct he entire QueryString.
Is there a way to assign the entire QueryString to a new String?
Something like
String newQuery = NavigationContext.QueryString.ToString();
I need to pass these parameters from the new page to another page but
don't want to reconstruct the entire QueryString
Why not? This is programming: do all the work in one place so you don't have to do it again later. Let's use an extension method to do this.
Silverlight
Place this code in a static class...
public string ToQueryString(this IDictionary dict)
{
string querystring = "";
foreach(string key in dict.AllKeys)
{
querystring += key + "=" + dict[key] + "&";
}
return querystring;
}
Use it like this...
string QueryString = NavigationContext.QueryString.ToQueryString();
ASP.NET
When I originally read this question, I thought it was for ASP.NET, not Silverlight. I'll leave the ASP.NET answer here in case someone stumbles across it looking for how to do it in ASP.NET.
public string ToQueryString(this NameValueCollection qs)
{
string querystring = "";
foreach(string key in qs.AllKeys)
{
querystring += key + "=" + qs[key] + "&";
}
return querystring;
}
Use it like this...
string QueryString = Request.QueryString.ToQueryString();
There is something that already exists for ASP.NET. But I feel it's important to demonstrate that you can do all the work once somewhere. Then not have to do it again. If you want to use a built-in way, something like this would work, using the Query property of the Uri class.
string QueryString = System.Web.HttpContext.Current.Request.Url.Query;
Here's a way that may be a little simpler...
You could project the results into a format of your choosing. Here's a simple example below.
I've used an IDictionary<string,string> as it is the underlying type for NavigationContext.QueryString
var test = new Dictionary<String,String>();
test.Add("1", "one");
test.Add("2", "two");
test.Add("3", "three");
// Choose any string format you wish and project to array
var newArray = test.Select(item => item.Key + ":" + item.Value).ToArray();
// Join on any separator
string output = String.Join(",", newArray);
This still means that you have to interpret the result later (according to the format you chose). Here you'll get a format like
"1:one,2:two,3:three"
If you've sent it as a querystring just pull it back out on the OnNavigatedTo() Method and then you can store the query in the page until you move on?.
string newQuery;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
newQuery = NavigationContext.QueryString["queryName"];
}
Try this:
public string GetQueryString()
{
IDictionary<String, String> NavigationContextData = NavigationContext.QueryString;
string data = "/Pagename.xaml?";
foreach (var item in NavigationContextData)
{
data += item.Key + "=" + item.Value + "&";
}
data = data.Substring(0, data.Length - 1);
return data;
}
If it's in your OnNavigatedTo() event, you can use a quick, easy two-liner. This can be condensed to a single line or expanded to check for the existence of the ? character. If you know that there are always parameters passed, the check is unnecessary and these two lines work fine:
string QStr = e.Uri.ToString();
string ParmStr = QStr.Substring(QStr.IndexOf('?') + 1);
You can also condense it into a single line:
string ParmStr = e.Uri.ToString().Substring(e.Uri.ToString().IndexOf('?') + 1);
I have a problem with getting a 'keyword' from this string, i tried string.replace() but it didn't work, has anyone any idea, how separate keyword from this string?
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
Thanks for help!
While you work with xml document, it will be easy to get values elements:
var xml = "<id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword</id>";
var url = XElement.Parse(xml).Value;
var index = url.LastIndexOf('/') + 1;
var keyword = url.Substring(index);
If you always need just last segment you can easily achieve with Url instance:
var keyword = new Uri(url).Segments.Last();
Thanks #Alexei
var url = "< id xmlns=\"http://www.w3.org/2005/Atom\">http://gdata.youtube.com/feeds/api/videos/keyword< /id>";
string[] splitArra = url.Split(new char[]{'/','<'});
string keywordString = splitArra[11];
I'm sure there's a better and cleaner way to this but this should work:
string keyword = url.Substring((url.IndexOf("videos/")) + 7,url.Length - url.IndexOf("< /id>")+1);
Or this:
string keyword = url.Substring(83, url.Length - url.IndexOf("< /id>") + 1);
I've got a bit of code that is giving me some trouble.
I'm trying to bundle a list of images into a zip file. The problem I'm having is that, on occasion, one of the images will be opened when it is accessed, causing an exception.
I'm pretty sure it's a timing issue, so I'm coding a 'second chance' loop to catch the images that fall through (as opposed to the existing behavior, where it halts on any error and gives back what it has thusfar).
I have the potentially erroring sections of code in a try block, as seen below
if (!Directory.Exists(physicalPath + "/" + fi.Description))
{
Directory.CreateDirectory(physicalPath + "/" + fi.Description);
}
wc.DownloadFile(source, physicalPath + "/" + fileName);
ze = zip.AddFile(physicalPath + "/" + fileName, path);
ze.FileName = fileName;
'ze' is a 'ZipEntry' from the Ionic.Zip library, and 'wc' is a WebClient.
In my catch, I need to store two pieces of information: 'source' and the string that results from 'physicalPath + "/" + filename'.
I know there's a way in .NET 4 to dynamically create a new object to hold this data, but I don't recall what it's called. This has greatly hampered my google-fu.
How can I create a dynamic object that will hold a pair of strings (preferably with property names on the variables) without creating a whole new class?
Are you referring to a Tuple?
http://msdn.microsoft.com/en-us/library/system.tuple.aspx
You can create an anonymous type like this:
var obj = new { str1 = "something", str2 = "something else" };
Console.WriteLine(string.Format("str1: {0}, str2: {1}", obj.str1, obj.str2));
Edit #1 To make an array of the anonymous type ...
var arr = new[] {
new { str1 = "a", str2 = "b" },
new { str1 = "c", str2 = "c" },
};
Edit #2 To make a Generic List out of this, you have to get a bit fancier ...
var obj = new { str1 = "", str2 = "" };
var type = typeof(List<>);
var listType = t.MakeGenericType(obj.GetType());
var list = (IList)Activator.CreateInstance(listType);
list.Add(new { str1 = "something", str2 = "something else" });
Console.WriteLine(((dynamic)list[0]).str1);
or a dynamic object? http://msdn.microsoft.com/en-us/library/dd264736.aspx
How can I create a dynamic object that will hold a pair of strings (preferably with property names on the variables) without creating a whole new class?
Use a Tuple.
// this uses a tuple to create a generic class that holds two string values.
// We also use string.Format instead of path + "/" + fileName to generate the second value.
var zipData = new Tuple<string, string>(source, string.Format("{0}/{1}", physicalPath, fileName));