Cообщество разработчиков на платформе Microsoft >
Форумы
>
Windows Presentation Foundation (WPF)
>
What's equivalent in WPF for "Application.Restart()" in WindowsForms?
What's equivalent in WPF for "Application.Restart()" in WindowsForms?
- Application.Curent object doesn't contain such method.
Any suggestions?
Thanks
Ответы
- Afaik, there isn't one in WPF.
Все ответы
- Afaik, there isn't one in WPF.
- Why a such method has been omitted in WPF?
It's very useful for ClickOnce (after an update) or a language change...
There is absolutly no alternative?
Thanks - Is there going to be one? Or equivalent functionality? How are you supposed to 'refresh' the application following a ClickOnce update?
Thanks
Nick - This feature may be added later on, but Application.Restart() is not hard to implement for your application. Use Reflector to see the WinForms implementation. ;-)
- As it turns out, Application.Restart() is quite hard to implement properly, as evidenced by using Reflector to inspect the WinForms implementation and discovering that it isn't actually implemented properly there, for a start. It completely ignores command line escaping when creating the command line to start the new process with. To demonstrate this, you can use some very short code just to show command args and perform a restart:
static void Main(string[] args)
{
Console.WriteLine("Restart Tester");
Console.WriteLine("command line: " + Environment.CommandLine);
Console.WriteLine("args:");
foreach (string arg in args)
Console.WriteLine("\t" + arg);
Console.WriteLine();
Console.WriteLine("Enter r to restart, or anything else to quit");
if(Console.ReadLine() == "r")
{
Application.Restart();
}
}
Open up a command prompt, and run: RestartTest.exe "te\" st" Due to the (admittedly obscure) command line escaping rules, the result of this is:
command line: RestartTest.exe "te\" st"
args:
te" st
If the application is then restarted using Application Restart, the escaping is ignored, and the restarted instance displays:
command line: "C:\...\RestartTest.exe" "te" st"
args:
te
st
If you want to do Restart properly, it's substantially trickier to get the command line to pass to the next instance. My current code for doing this, which I hope handles all cases, is:
/// <summary>
/// Gets the command args this app was started with, as executed, not parsed into args[]
/// </summary>
public static string GetCommandArgs()
{
string commandLine = Environment.CommandLine;
//Find either the space, which is a the delimeter, or a " mark, which bounds spaces
int pos = 0;
do
{
if (pos >= commandLine.Length)
return String.Empty; //No command line args
pos = commandLine.IndexOfAny(new char[] { ' ', '"' }, pos);
if (pos == -1)
return String.Empty; //No command line args
if (commandLine[pos] == '"')
{
//Find the closing " mark. " marks can't be escaped in path names
pos = commandLine.IndexOf('"', pos + 1) + 1;
if (pos == 0)
{
//No command line args. Probably malformed command line too.
System.Diagnostics.Trace.TraceWarning("Could not find closing \" mark in command line: " + commandLine);
return String.Empty;
}
//Otherwise, go round again to find another quote, or alternatively a space
}
else
{
System.Diagnostics.Debug.Assert(commandLine[pos] == ' ', "Expecting a space here, if not a \" mark");
//Everything past this point should now be the command args, as this is an unquoted space
return commandLine.Substring(pos + 1);
}
} while (true);
}
I hope this helps, and if anyone has further refinements on the above, or notices cases for which it does not work, I'd be interested to hear them.
Alex - As far as I know you could add a reference to System.Windows.Forms and add these code statements:
System.Windows.Forms.Application.Restart(); Application.Current.Shutdown();
- Предложено в качестве ответаJunior Mayhe 23 июля 2009 г. 18:50
IMO, We should never reference to WIndows.Form.
Better to use the code like that below ....
System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
Application.Current.Shutdown();
Michael Sync: blog: http://michaelsync.net

