views:

107

answers:

3

Hey everybody,

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

+1  A: 

Create a Windows Form Application instead of Console Application and replace all the code in Main method of program.cs with

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

Delete Form1.cs

Faisal
Well, the application from which this code is being called is a WinForm app!
+1  A: 

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;
    }
Shaihi
+1  A: 

Thank you very much Shaihi, you set me on the right track.

The code and links you provided got me finally to the following solution that works fine for me:

[DllImport("coredll.dll")]
    private static extern int WNetAddConnection3(IntPtr hWndOwner,
    ref NetResource lpNetResource, string lpPassword, string lpUserName, int dwFlags);

 [DllImport("coredll.dll")]
    static extern int WNetCancelConnection2(string lpName, Int32 dwFlags, bool bForce);
...
try
{

   NetResource logsResource = new NetResource();
   logsResource.lpLocalName = "logs";
   logsResource.lpRemoteName = @"\\server\logs";
   logsResource.dwType = 0x1; //const int RESOURCETYPE_DISK = 0x1
   logsResource.dwScope = 0;
   logsResource.dwUsage = 0;
   logsResource.dwDisplayType = 0;

   //try to connect the network resource
   WNetAddConnection3(new IntPtr(0), ref logsResource, @"pass", @"dom\user", 0);

   //copy files to the server
   string[] logfiles = Directory.GetFiles(@"\System\Logs\");
   foreach (string logfile in logfiles)
   {
       File.Copy(logfile, @"\network\logs\" + 
                 logfile.Substring(logfile.LastIndexOf(@"\") + 1), true);
   }
}
catch
{
}
finally
{
    //try to disconnect network resource
    WNetCancelConnection2("logs", 0, false);
}

The two WNET function calls return an interger value. If this value equals to 0 the operation finished successfully. Common codes I experienced are 53 and 85. Refer to this list to get a clue what the numbers mean!