-1

I am starting a process to disconnect a network drive, but it always pops up a command prompt window. It just flashes by quickly and disappears, but i would like to configure it so the window doesn't open at all. Any ideas?

Here is the c# code I'm currently using:

private void btnDisconnectNetwork_Click(object sender, EventArgs e)
{
    Process DisconnectDrive = new Process();
    DisconnectDrive.StartInfo.FileName = "Net.exe";
    DisconnectDrive.StartInfo.Arguments = @" Use /d Q:";
    DisconnectDrive.StartInfo.CreateNoWindow = true;
    DisconnectDrive.Start();
}
vesan
  • 3,289
  • 22
  • 35
  • 1
    _"Any ideas?"_ - Perform the network drive disconnection _in code_ rather than spawn a child process. –  Oct 06 '15 at 00:47
  • i would like to do that, but i don't know how. I'm kind of a newbie. can you point me in the right direction for doing in code? – user3758081 Oct 06 '15 at 00:49
  • [Would this code which shows how to map the drive and unmap it dynamically at runtime do?](http://stackoverflow.com/questions/1808226/reconnecting-a-disconnected-network-drive) –  Oct 06 '15 at 00:57
  • No, i will have the user select a UNC from a combo box and from that i will map the "Q:" drive letter to that UNC. – user3758081 Oct 06 '15 at 01:09
  • Although spawning a child process is not the ideal way to do it, I would still like to know if there is a way to do this through Net.exe without showing the prompt window that pops up. – user3758081 Oct 06 '15 at 01:11
  • I think i found what i was looking for: DisconnectDrive.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; – user3758081 Oct 06 '15 at 01:21
  • 1
    Possible duplicate of [how to start process with hidden window](http://stackoverflow.com/questions/22635340/how-to-start-process-with-hidden-window) – Jacob Krall Oct 06 '15 at 01:22

1 Answers1

0

I believe the following will also work:

using System.Diagnostics;

namespace processexample {
class Program {
    static void Main(string[] args) {
        ProcessStartInfo si = new ProcessStartInfo();
        si.CreateNoWindow = true;
        si.UseShellExecute = false;
        si.FileName = @"C:\Windows\System32\net.exe";
        si.Arguments = @"/help";
        Process p = new Process();
        p.StartInfo = si;
        p.Start();
        }
    }
}

You have to set the CreateNoWindow and UseShellExecute in StartInfo.

Dweeberly
  • 4,668
  • 2
  • 22
  • 41