How to start a Wi-Fi network using C #

I created a shortcut in the C: \ Temp folder to connect to a Wi-Fi network (a special kind of short cut)

I am trying to run 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 "UseShellExecutable = False" and "RedirectStandardError = True", I get an exception: "The specified executable is not a valid Win32 application"

I tried to find the executable by outputting the "FindExecutable ()" method, but it returned an empty string.

Any help is greatly appreciated.

+3
source share
5 answers

You are missing a colon on your way. I created a shortcut on my desktop and then did 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();
+1
source

Yes, I confirmed that this does not work on WinXP. If you check the shortcuts tab in the lnk file, you will find that the targettype is actually a GUID (which maps to a pointer for the specified network card).

My assumption is that the necessary guid translation is not handled properly by the shell when process.start is used under XP. You may have to try another way to launch a shortcut under XP, for example, by calling Win32 com to launch a shortcut. check pinvoke site for function header.

: FindExecutable, http://www.pinvoke.net/default.aspx/shell32.ShellExecute

cmd.exe/k, . pinvoke .bat - , , .

+1

, shell.lnk?

rundll32?

0
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;

    }
  }
}

wifi.lnk?

0

, GUID, FindExecutable , , :

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

, "start wifi.lnk" , :

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();
    }
}

- , ?

, "start wifi.lnk" , kludge.

0

Source: https://habr.com/ru/post/1712042/


All Articles