Search code examples
c#.netcompact-frameworkwindows-ceshellexecute

Run command line program silently with c# on wince


I once again need some help.

I'm using the .net Compact Framework and the programming language C# to develop for mobile devices that are running WinCE 5.0.

What I want to accomplish is to programmatically mount a network drive. To do so, the app runs the following code in a background thread:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "net";
startInfo.UseShellExecute = false;
startInfo.Arguments = @"use logs \\server\logs /user:dom\uname /password:pw";
Process p = Process.Start(startInfo);
p.WaitForExit(5000);

Now my problem is, that this code will display a console in the foreground and writes the command to it and the answer from the command as well. Also, the console won't disappear anymore.

The parameter 'UseShellExecute' doesn't seem to show any effect.

I've read about the parameter 'CreateNoWindow', but it doesn't exist in the compact framework.

So folks, is there a possibility to run net-commands in the background, the user shouldn't notice and certainly not see the command including the password in plain text.

I hope you get the idea.

Many thanks in advance
Toby


Solution

  • You can use WNetAddConnetion3 by P/Invoking it (here is the declaration).Here is the declaration for the NetResource struct:

        [StructLayout(LayoutKind.Sequential)]
        internal struct NetResource
        {
            public uint dwScope;
            public uint dwType;
            public uint dwDisplayType;
            public uint dwUsage;
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public string lpLocalName;
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public string lpRemoteName;
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public string lpComment;
            [MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
            public string lpProvider;
        }