-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
52 lines (51 loc) · 1.8 KB
/
Program.cs
File metadata and controls
52 lines (51 loc) · 1.8 KB
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
/*
** ----------------------------------------------------------------------------
** Filename: addpath.cs
** Version: 1
** Revision: 0
** Release: 2019-10-19
** Author: Arnold Sebr <arnold@sebr.name>
** ----------------------------------------------------------------------------
** (C) Copyright 2019 by Arnold Sebr. All rights reserved.
** ----------------------------------------------------------------------------
*/
/*
** includes
*/
using System ;
using System.Collections.Generic ;
using System.Text ;
using System.Text.RegularExpressions ;
using Microsoft.Win32 ;
/*
** AddPath Program Main
*/
namespace AddPath {
class Program {
static void Main( string[ ] args ) {
string RegKey = Registry.LocalMachine + @"\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ;
Console.WriteLine( "AddPath, version 1.0, (C) Copyright 2019 by Arnold Sebr. All rights reserved.\n" ) ;
if( args.Length == 0 ) {
Console.WriteLine( "usage: AddPath <directory>\n" ) ;
Console.WriteLine( "Appends <directory> to the environment path variable." ) ;
Console.WriteLine( "f.e.: AddPath D:\\my\\new\\path" ) ;
Environment.Exit( 0 ) ;
}
string newPath = args[ 0 ] ;
if( !Regex.IsMatch( newPath , "^(([a-zA-Z]\\:)|(\\\\))(\\\\{1}|((\\\\{1})[^\\\\]([^/:*?<>\"|]*))+)$" , RegexOptions.IgnoreCase ) ) {
Console.WriteLine( "error: invalid directory: " + newPath ) ;
Environment.Exit( 0 ) ;
}
try {
string oldPath = (string) Registry.GetValue( RegKey , "Path" , string.Empty ) ;
Registry.SetValue( RegKey , "Path" , string.IsNullOrEmpty( oldPath) ? newPath : oldPath + ';' + newPath , RegistryValueKind.ExpandString ) ;
}
catch( Exception ex ) {
Console.WriteLine( "error: " + ex.Message ) ;
}
}
}
}
/*
** EOF
*/