AustinWise (owner)

Revisions

gist: 123373 Download_button fork
public
Public Clone URL: git://gist.github.com/123373.git
Embed All Files: show embed
MapDrivesService.cs #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Woah, full project now here:
// http://github.com/AustinWise/MapDrives/tree/master
 
 
 
 
 
 
using System.Diagnostics;
using System.ServiceProcess;
 
namespace MapDrive
{
    public partial class MapDrivesService : ServiceBase
    {
        public MapDrivesService()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            foreach (var line in Properties.Settings.Default.DrivesToMap)
            {
                var splitLine = line.Split('|');
                var driveLetter = splitLine[0];
                var destPath = splitLine[1]; //the path should not end in a slash
                unmapDrive(driveLetter);
                mapDrive(driveLetter, destPath);
            }
        }
 
        protected override void OnStop()
        {
            foreach (var line in Properties.Settings.Default.DrivesToMap)
            {
                var splitLine = line.Split('|');
                var driveLetter = splitLine[0];
                var destPath = splitLine[1];
                unmapDrive(driveLetter);
            }
        }
 
        private void unmapDrive(string letter)
        {
            Process.Start("net.exe", string.Format("use {0}: /delete", letter)).WaitForExit();
        }
 
        private void mapDrive(string letter, string path)
        {
            Process.Start("net.exe", string.Format("use {0}: \"{1}\"", letter, path)).WaitForExit();
        }
    }
}