I'm registering a custom protocol handler on my computer, which calls this application:
string prefix = "runapp://";
// The name of this app for user messages
string title = "RunApp URL Protocol Handler";
// Verify the command line arguments
if (args.Length == 0 || !args[0].StartsWith(prefix))
{
MessageBox.Show("Syntax:\nrunapp://<key>", title); return;
}
string key = args[0].Remove(0, "runapp://".Length);
key.TrimEnd('/');
string application = "";
string parameters = "";
string applicationDirectory = "";
if (key.Contains("~"))
{
application = key.Split('~')[0];
parameters = key.Split('~')[1];
}
else
{
application = key;
}
applicationDirectory = Directory.GetParent(application).FullName;
ProcessStartInfo psInfo = new ProcessStartInfo();
psInfo.Arguments = parameters;
psInfo.FileName = application;
MessageBox.Show(key + Environment.NewLine + Environment.NewLine + application + " " + parameters);
// Start the application
Process.Start(psInfo);
What it does is that it retrieves the runapp:// request, split it into two parts: application and the parameters passed, according to the location of the '~' character. (This is probably not a good idea if I ever pass PROGRA~1 or something, but considering I'm the only one using this, it's not a problem), then runs it.
However, a trailing '/' is always added to the string: if I pass
runapp://E:\Emulation\GameBoy\visualboyadvance.exe~E:\Emulation\GameBoy\zelda4.gbc, it will be interpreted as
runapp://E:\Emulation\GameBoy\visualboyadvance.exe E:\Emulation\GameBoy\zelda4.gbc/.
Why would it do this ? And why can't I get rid of this trailing slash ? I tried TrimEnd('/'), Remove(key.IndexOf('/'), 1), Replace("/", ""), yet the slash stays. What is happening ?
You need to assign the result of the TrimEnd:
key = key.TrimEnd('/');
Strings in C# are immutable; therefore string methods which alter the string return a new string with the alterations, rather than changing the original string.
Related
I'm trying to pass command-line arguments to a C# application, but I have problem passing something like this
"C:\Documents and Settings\All Users\Start Menu\Programs\App name"
even if I add " " to the argument.
Here is my code:
public ObjectModel(String[] args)
{
if (args.Length == 0) return; //no command line arg.
//System.Windows.Forms.MessageBox.Show(args.Length.ToString());
//System.Windows.Forms.MessageBox.Show(args[0]);
//System.Windows.Forms.MessageBox.Show(args[1]);
//System.Windows.Forms.MessageBox.Show(args[2]);
//System.Windows.Forms.MessageBox.Show(args[3]);
if (args.Length == 3)
{
try
{
RemoveInstalledFolder(args[0]);
RemoveUserAccount(args[1]);
RemoveShortCutFolder(args[2]);
RemoveRegistryEntry();
}
catch (Exception e)
{
}
}
}
And here is what I'm passing:
C:\WINDOWS\Uninstaller.exe "C:\Program Files\Application name\" "username" "C:\Documents and Settings\All Users\Start Menu\Programs\application name"
The problem is I can get the first and the second args correctly, but the last one it gets as C:\Documents.
Any help?
I just ran a check and verified the problem. It surprised me, but it is the last \ in the first argument.
"C:\Program Files\Application name\" <== remove the last '\'
This needs more explanation, does anybody have an idea? I'm inclined to call it a bug.
Part 2, I ran a few more tests and
"X:\\aa aa\\" "X:\\aa aa\" next
becomes
X:\\aa aa\
X:\\aa aa" next
A little Google action gives some insight from a blog by Jon Galloway, the basic rules are:
the backslash is the escape character
always escape quotes
only escape backslashes when they precede a quote.
To add Ian Kemp's answer
If you assembly is called "myProg.exe" and you pass in the string "C:\Documents and Settings\All Users\Start Menu\Programs\App name" link so
C:\>myprog.exe "C:\Documents and Settings\All Users\Start Menu\Programs\App name"
the string "C:\Documents and Settings\All Users\Start Menu\Programs\App name"
will be at args[0].
To add to what everyone else has already said, It might be an escaping problem. You should escape your backslashes by another backslash.
Should be something like:
C:\>myprog.exe "C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\App name"
I noticed the same annoying issue recently, and decided to write a parser to parse the command line arguments array out myself.
Note: the issue is that the .NET CommandLine Arguments passed to the static void Main(string[] args) function escapes \" and \\. This is by design, since you may actually want to pass an argument that has a quote or backslash in it. One example:
say you wanted to pass the following as a single argument:
-msg:Hey, "Where you at?"
eg.
sampleapp -msg:"Hey, \"Where you
at?\""
Would be how to send it with the default behavior.
If you don't see a reason for anyone to have to escape quotes or backslashes for your program, you could utilize your own parser to parse the command line, as below.
IE.
[program].exe "C:\test\" arg1 arg2
would have a args[0] = c:\test" arg1 arg2
What you would expect is args[0]=c:\test\ and then args[1]=arg1 and args[2]=arg2.
The below function parses the arguments into a list with this simplified behavior.
Note, arg[0] is the program name using the below code. (You call List.ToArray() to convert the resulting list to a string array.)
protected enum enumParseState : int { StartToken, InQuote, InToken };
public static List<String> ManuallyParseCommandLine()
{
String CommandLineArgs = Environment.CommandLine.ToString();
Console.WriteLine("Command entered: " + CommandLineArgs);
List<String> listArgs = new List<String>();
Regex rWhiteSpace = new Regex("[\\s]");
StringBuilder token = new StringBuilder();
enumParseState eps = enumParseState.StartToken;
for (int i = 0; i < CommandLineArgs.Length; i++)
{
char c = CommandLineArgs[i];
// Console.WriteLine(c.ToString() + ", " + eps);
//Looking for beginning of next token
if (eps == enumParseState.StartToken)
{
if (rWhiteSpace.IsMatch(c.ToString()))
{
//Skip whitespace
}
else
{
token.Append(c);
eps = enumParseState.InToken;
}
}
else if (eps == enumParseState.InToken)
{
if (rWhiteSpace.IsMatch(c.ToString()))
{
Console.WriteLine("Token: [" + token.ToString() + "]");
listArgs.Add(token.ToString().Trim());
eps = enumParseState.StartToken;
//Start new token.
token.Remove(0, token.Length);
}
else if (c == '"')
{
// token.Append(c);
eps = enumParseState.InQuote;
}
else
{
token.Append(c);
eps = enumParseState.InToken;
}
}
//When in a quote, white space is included in the token
else if (eps == enumParseState.InQuote)
{
if (c == '"')
{
// token.Append(c);
eps = enumParseState.InToken;
}
else
{
token.Append(c);
eps = enumParseState.InQuote;
}
}
}
if (token.ToString() != "")
{
listArgs.Add(token.ToString());
Console.WriteLine("Final Token: " + token.ToString());
}
return listArgs;
}
In response to WWC's answer, Jamezor commented that his code will fail if the first character is a quote.
To fix that problem, you can replace the StartToken case with this:
if (eps == enumParseState.StartToken)
{
if (rWhiteSpace.IsMatch(c.ToString()))
{
//Skip whitespace
}
else if (c == '"')
{
eps = enumParseState.InQuote;
}
else
{
token.Append(c);
eps = enumParseState.InToken;
}
}
What exactly is the problem? Anyway here's some general advice:
Make sure your Main method (in Program.cs) is defined as:
void Main(string[] args)
Then args is an array containing the command-line arguments.
I'm trying to add your computer name to the name of the plugin text. Here is a example:
I have a path which detects file which is here:
string pypath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
+ "\\elfen_encore\\extra_maya\\mayaplugins\\CoDMayaTools.py";
From there I use this code to access a string in there
public void changepy()
{
if (File.Exists(pypath))
{
{
string quotes = "\"\"";
string name = System.Environment.MachineName;
string text = File.ReadAllText(pypath);
text = text.Replace("\"Call of Duty Tools\"", quotes + name);
File.WriteAllText(pypath, text + name);
}
MessageBox.Show("Changed ");
}
else
{
}
Then this is the file it should change to computer name :
OBJECT_NAMES = {'menu' : ["CoDMayaToolsMenu",
"Call of Duty Tools", None, None, None],
"CoDMayaToolsMenu" is the issue; I want to replace that with the users computer name but as you can see its in quotes and I am having huge issues on trying to get the text in the quotes. How can I solve it?
Is this what you are trying to do?
text = text.Replace("\"Call of Duty Tools\"", "\"" + name + "\"");
If not, please specify a little bit more your question or your desired output.
If you're leaving the quotes anyway, why not this approach? ... It's a bit unclear if this is what you're trying to accomplish?
public void changepy()
{
if (File.Exists(pypath))
{
string machineName = System.Environment.MachineName;
string content = File.ReadAllText(pypath);
content = content.Replace("Call of Duty Tools", machineName);
File.WriteAllText(pypath, content);
MessageBox.Show("Changed");
}
else
{
}
}
I have the following code in C#:
if (function.Equals("Larger50"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
if (result.Equals("True") || result.Equals("true"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab')", true);
}
if(result.Equals("False") || result.Equals("false"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/Smaller.aspx', '_newtab')", true);
}
if(result.Equals("Error") || result.Equals("error"))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.open('http://localhost:4000/ErrorPage.htm', '_newtab')", true);
}
Session["result"] = result;
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, "window.location.href = 'Results.aspx'", true);
}
The result variable can have any one of three values (if the server responds):
i) true
ii) false
iii) error
The main problem with this code is that the new tab script in each of the three if statements work as they should. However, the last script which opens the Results.aspx page is not executing for some reason or another. The script is written well as it executed perfectly if all the other code is commented out. How should I solve the problem?
I tried replacing it with Response.Redirect("Results.aspx") however then this exeuctes and all the other three scripts never execute.
You should register these all at once, rather than in two separate statements:
if (function.Equals("Larger50"))
{
Request req = new Request();
string result = req.doRequest("function=" + function + "&num=" + number, "http://localhost:4000/Handler.ashx");
string scriptVal = "";
if (result.Equals("True") || result.Equals("true"))
{
scriptVal = "window.open('http://localhost:4000/Larger.aspx?num=" + number + "', '_newtab');";
}
if(result.Equals("False") || result.Equals("false"))
{
scriptVal = "window.open('http://localhost:4000/Smaller.aspx', '_newtab');";
}
if(result.Equals("Error") || result.Equals("error"))
{
scriptVal = "window.open('http://localhost:4000/ErrorPage.htm', '_newtab');";
}
Session["result"] = result;
scriptVal += "window.location.href = 'Results.aspx';";
Page.ClientScript.RegisterStartupScript(Page.GetType(), null, scriptVal, true);
}
See the docs on ClientScriptManager.RegisterStartupScript, specifically:
A startup script is uniquely identified by its key and its type.
Scripts with the same key and type are considered duplicates. Only one
script with a given type and key pair can be registered with the page.
Attempting to register a script that is already registered does not
create a duplicate of the script.
In your case, the type and key are the same in both of the scripts you register.
You could uniquely identify them with a key, and then register them separately. But you have to keep in mind that the order of execution is not guaranteed:
The script blocks are not guaranteed to be output in the order they
are registered.
How can I have a button in my desktop application that causes the user's default browser to launch and display a URL supplied by the application's logic.
Process.Start("http://www.google.com");
Process.Start([your url]) is indeed the answer, in all but extremely niche cases. For completeness, however, I will mention that we ran into such a niche case a while back: if you're trying to open a "file:\" url (in our case, to show the local installed copy of our webhelp), in launching from the shell, the parameters to the url were thrown out.
Our rather hackish solution, which I don't recommend unless you encounter a problem with the "correct" solution, looked something like this:
In the click handler for the button:
string browserPath = GetBrowserPath();
if (browserPath == string.Empty)
browserPath = "iexplore";
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = "\"" + [whatever url you're trying to open] + "\"";
process.Start();
The ugly function that you shouldn't use unless Process.Start([your url]) doesn't do what you expect it's going to:
private static string GetBrowserPath()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
// try location of default browser path in XP
key = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
// try location of default browser path in Vista
if (key == null)
{
key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http", false); ;
}
if (key != null)
{
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
key.Close();
}
}
catch
{
return string.Empty;
}
return browser;
}
to all.
i developing a webpage using asp.net with c# language, in this webpage i have a textbox for taking url of the domain and button. when user enter domain name in the textbox and press the button the details of the domain will displaying in the other window. i take help from stackoverflow user and i get the code it is working fine, but when i type the domain name particularly ".in" doamins are not giving details. simply domain available message is displaying actually the domain is registered for example i tried "axisbank.co.in" in my page it is displaying the domain is available but actually it is already taken. I am sending my code please help me ( particularly .in domain names)
protected void Button1_Click(object sender, EventArgs e)
{
lblDomainName.Text = Session["WhoIs"].ToString();
string firstLevelbufData = null;
// Stores the bufData extracted from the webclient
try
{
// similarly we can select any server address for bufData mining
string strURL = "http://www.directnic.com/whois/index.php?query=" + txtDomain.Text;
WebClient web = new WebClient();
// byte array to store the extracted bufData by webclient
byte[] bufData = null;
bufData = web.DownloadData(strURL);
// got the bufData now convert it into string form
firstLevelbufData = Encoding.Default.GetString(bufData);
}
catch (System.Net.WebException ex)
{
// this exception will be fired when the host name is not resolved or any other connection problem
//txtResult.Text = ex.Message.ToString();//sasi
lblresult.Text = ex.Message.ToString();
return;
}
try
{
// first and last are the regular expression string for extraction bufData witnin two tags
// you can change according to your requirement
string first = null;
string last = null;
// chr(34) is used for (") symbol
first = "<p class=\"text12\">";
last = "</p>";
Regex RE = new Regex(first + "(?<MYDATA>.*?(?=" + last + "))", RegexOptions.IgnoreCase | RegexOptions.Singleline);
// try to extract the bufData within the first and last tag
Match m = RE.Match(firstLevelbufData);
// got the result
//txtResult.Text = m.Groups["MYDATA"].Value + "<br>";//sasi
lblresult.Text = m.Groups["MYDATA"].Value + "<br>";
// check if no information abour that domain is available
//if (txtResult.Text.Length < 10) txtResult.Text = "Domain "+ txtDomain .Text +" is Available";//sasi
if (lblresult.Text.Length < 10)
lblresult.Text = "Domain " + txtDomain.Text + " is Available";
}
catch (System.Net.WebException ex)
{
lblresult.Text = " Sorry the information is currently not available !! ";
}
}
help me thank you
http://www.directnic.com
does not have information about .co.in domain names.
Most of the whois sites won't allow you to fetch the results before filling in CAPTCHA.
http://registry.in/ is the official registry, try using whois protocol at whois.registry.in