tags:

views:

954

answers:

5

Hello,

I have created a shortcut in C:\Temp folder for Wifi Network connection (special kind of short cut)

I am trying to launch this using C#


System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.StartInfo.FileName = "C:\\Temp\\wifi.lnk";
myProc.Start();

When I run the above code, nothing really happens. when I set the "UseShellExecutable = False" and "RedirectStandardError = True", I am getting an exception saying "The specified executable is not a valid Win32 application"

I have tried to find the executable by pinvoking the "FindExecutable()" method, but it returns empty string.

Any help is greatly appreciated.

A: 

Perhaps if you shell execute the .lnk?

Use rundll32?

Christopher Karper
I have tried the following code, and it has no impact (won't display the wireless network settings dialog)<pre>System.Diagnostics.Process myProc = new System.Diagnostics.Process();myProc.StartInfo.FileName = "rundll32.exe";myProc.StartInfo.Arguments= "C\\Temp\\wifi.lnk";myProc.Start();</pre>Any help is greatly appreciated
+1  A: 

You're missing a colon in your path. I created the shortcut on my desktop, and then ran the following, and it worked as expected...

System.Diagnostics.Process myProc = new System.Diagnostics.Process();
myProc.StartInfo.FileName = @"C:\Users\scott\Desktop\wifi.lnk";
myProc.Start();
Scott Ivey
Did you create a short cut for "Wireless Network Connection" under network connections?It works fine with any regular short cuts for an executable OR for a file. This doesn't seem to work special non-file based short cuts.Any help is greatly appreciated.
i opened up my network connections, and dragged my wireless connection to the desktop. Once there, i renamed it to "wifi". As an aside, i'm not running XP - i'm running Win7 - but i can't think of any reason that should make any difference.
Scott Ivey
Works under Vista both to file and non file short cuts.
Jonas Rindberg
A: 
using System;
// add a reference to the com component
// "Windows Script Host Object Model" for IWshRuntimeLibrary
using IWshRuntimeLibrary;

namespace ConsoleApplicationCSharp
{  
  public class Foo
  {
    public static void Main(string[] args)
    {
      string pathLnk = @"C:\Users\scott\Desktop\wifi.lnk";

      WshShell shell = new WshShell();
      IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(pathLnk);
      Console.WriteLine("target path: " + shortcut.TargetPath);
      Console.WriteLine("argument: " + shortcut.Arguments);
      Console.WriteLine("working dir: " + shortcut.WorkingDirectory);
      return;

    }
  }
}

Is this code able to extract the information from your wifi.lnk?

VolkerK
returns "" for shortcut.TargetPath, shortcut.Arguments and shortcut.WorkingDirectory
+1  A: 

Yeah I have confirmed this doesn't work on WinXP. If you check the shortcut tab of the lnk file, you will find that the targettype is actually a GUID (which maps to the guid for the specified network card).

My guess is that the necessary guid translation isn't being handled properly by the shell when process.start is used under XP. You might have to try a different way of starting the shortcut under XP, such as using a Win32 com interop call to start the shortcut. check out pinvoke website for function header.

Edit: Actually i wasn't refering to the FindExecutable signature, I was refering to http://www.pinvoke.net/default.aspx/shell32.ShellExecute

also tried cmd.exe /k and that doesn't work either. pinvoke or .bat file are your only friends it seems bradman.

Thanks anonymousType.I have tried to use ShellExecute as shown below ShellExecute(IntPtr.Zero, "open", myLnk, null, null, ShowCommands.SW_NORMAL) This doesn't seem to work, So I have changed the params as shown below ShellExecute(IntPtr.Zero, "open", "explorer", myLnk, null, ShowCommands.SW_NORMAL) With above call, I get a security warning, clickin on Open button opens the dialog successfully. Not sure how to suppress warning dialog?Any help is greatly appreciated.
A: 

As previously mentioned, the target of the shortcut is a GUID, so FindExecutable won't be able to help, but here's the signature for it if you're interested:

    [DllImport("shell32.dll")]
    static extern IntPtr FindExecutable(string file, string directory, [Out] StringBuilder result);

Interestingly, running "start wifi.lnk" works from a command prompt, but this doesn't:

class Program
{        
    static void Main(string[] args)
    {
        Process p = new Process();
        p.StartInfo.Arguments = "/c start wifi.lnk";
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.WorkingDirectory = @"C:\Documents and Settings\Administrator\Desktop";
        p.Start();
    }
}

Anyone want to venture a guess as to why?

If you really, really, really need this working, you could put the "start wifi.lnk" into a batch file and launch that from your program, but it's definitely a kludge.

jasonh
Thanks jasonh.running "start wifi.lnk" from command prompt works only ONCE. Once you close the dialog and try again, it wont display the wireless network settings dialog again. I am not sure why?I have tried to use ShellExecute as shown belowShellExecute(IntPtr.Zero, "open", myLnk, null, null, ShowCommands.SW_NORMAL)This doesn't seem to work, So I have changed the params as shown belowShellExecute(IntPtr.Zero, "open", "explorer", myLnk, null, ShowCommands.SW_NORMAL)With above call, I get a security warning, clickin on Open button opens the dialog successfully. how to suppress warni
Ah, I'm pretty sure you *can't* disable that security warning. Since you're opening it with Explorer, you're forced to rely on Explorer's security mechanisms, which prompt the user before opening random shortcuts under certain circumstances. If you' could override the warning, it's not much of a security barrier is it? ;) The batch file isn't really all that bad of a method. You can keep the code in your program, write it to a temporary file and then hide the resulting command window with the ProcessStartInfo.WindowStyle property. Still, a kludge, but it would be transparent, no?
jasonh
Also, I'm not sure why `start wifi.lnk` at the command prompt is only working once for you. I can do it over and over and over... with no difficulty.
jasonh
If the wireless network is connected, using "start wifi.lnk" at the command prompt seems to work only once. Can you check if this is true in your case?
Odd, if I'm connected to a WiFi network, it only works once.
jasonh