-1

I map a network drive, but after opening the application the network drive must be unmapped again.

I build my application in VS2015 c#.

private void button4_Click(object sender, EventArgs e)
{
    IWshNetwork_Class network = new IWshNetwork_Class();

    network.MapNetworkDrive("k:", @"\\10.*.*.*\d$\test", Type.Missing, "local\\blabla", "*******");

    System.Diagnostics.Process.Start("file:///K:\\gemy.exe");

    //This is the closing part
    network.RemoveNetworkDrive("k:");
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

You could probably check if the drive exists or not. See (Check Drive Exists(string path))

    using System.IO;

    private IWshNetwork_Class network = new IWshNetwork_Class();

    private void button4_Click(object sender, EventArgs e)
    {  
      string drive = Path.GetPathRoot("k:");

      if(!Directory.Exists(drive))
      {               
       network.MapNetworkDrive(drive, @"\\10.*.*.*\d$\test", Type.Missing, "local\\blabla", "*******");

       System.Diagnostics.Process.Start("file:///K:\\gemy.exe");                        
      }
      else
      {  
        //This is the closing part
        network.RemoveNetworkDrive("k:");
      }
    }

In case Directory Exists always responds false you might want to take a look at this: Check if directory exists on Network Drive

Community
  • 1
  • 1
usselite
  • 846
  • 7
  • 24
  • I get three errors. Severity Code Description Project File Line Error CS0103 The name 'Path' does not exist in the current context CAASapp C:\Users\Documents\Visual Studio 2015\Projects\CAASapp\CAASapp\Form1.cs Error CS0103 The name 'Directory' does not exist in the current context CAASapp C:\Users\Documents\Visual Studio 2015\Projects\CAASapp\CAASapp\Form1.cs Error CS0103 The name 'network' does not exist in the current context CAASapp C:\Users\Documents\Visual Studio 2015\Projects\CAASapp\CAASapp\Form1.cs – Clinton van Axel Nov 24 '15 at 16:49
  • I updated my answer. The Directory and Path error is related because you don't reference to System.IO (added it to answer) and the network error is related to not initializing it at the right place (also fixed in answer). – usselite Nov 25 '15 at 08:04
  • I have to press the button to close the mapped network drive is it possible to close it when i close the entire application. – Clinton van Axel Nov 25 '15 at 09:08
  • You can use events for that. You can find that easily on the internet. I hope my answer was of use to you. – usselite Nov 25 '15 at 09:21