이런걸 한번 만들어봤습니다.
이 프로그램은 간단하게 Golang 환경변수를 세팅해주는 프로그램입니다.
위 스샷 처럼 말이죠.
이걸 만들게 된 시작은 CMD setx 명령으로 환경변수를 등록할때 %path% 를 사용하면 사용자변수의 path와 시스템변수의 path가 합쳐져서 시스템변수로 들어가는 문제 때문입니다.
이래저래 고민해보다가 cmd 명령으론 방법이 없다는걸 깨닫고 c#으로 만들게 됬습니다.
using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace GolangSetEnvironmentVariables { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private RegistryKey regCU = Registry.CurrentUser; private String m_strGOROOT = "Golang 설치경로를 넣어주세요"; private String m_strGOPATH = "C:\\Develop\\Projects\\GoWorks"; private String m_strPATH = ""; public MainWindow() { InitializeComponent(); RegistryKey regInstallPath = regCU.OpenSubKey("Software\\GoProgrammingLanguage"); if (regInstallPath != null) { m_strGOROOT = regInstallPath.GetValue("installLocation").ToString(); } this.textBoxGoRoot.Text = m_strGOROOT; this.textBoxGoPath.Text = m_strGOPATH; } private void buttonURL_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("https://linsoo.co.kr"); } private void buttonRegist_Click(object sender, RoutedEventArgs e) { bool chkPath = true; m_strGOROOT = this.textBoxGoRoot.Text; m_strGOPATH = this.textBoxGoPath.Text; DirectoryInfo diGOROOT = new DirectoryInfo(m_strGOROOT); if (diGOROOT.Exists == false) { MessageBox.Show("\""+ m_strGOROOT + "\""+ "가 존재하지 않습니다."); chkPath = false; } DirectoryInfo diGOPATH = new DirectoryInfo(m_strGOPATH); if (diGOPATH.Exists == false) { MessageBox.Show("\""+m_strGOPATH + "\"" + "가 존재하지 않습니다."); chkPath = false; } if(chkPath == true) { diGOROOT = null; diGOPATH = null; RegistryKey regTemp = regCU.OpenSubKey("Environment", true); if (regTemp != null) { m_strPATH = regTemp.GetValue("path").ToString(); m_strPATH = m_strPATH.Trim(); int length = m_strPATH.Length; String tmp= m_strPATH.Substring(length - 1, 1); if(tmp != ";") { m_strPATH += ";"; } m_strPATH += m_strGOROOT; m_strPATH += "\\bin;"; m_strPATH += m_strGOPATH; m_strPATH += "\\bin"; regTemp.SetValue("GOROOT", m_strGOROOT, RegistryValueKind.String); regTemp.SetValue("GOPATH", m_strGOPATH, RegistryValueKind.String); regTemp.SetValue("path", m_strPATH, RegistryValueKind.String); MessageBox.Show("등록했습니다."); regTemp.Close(); } } } } }
MainWindow.xaml.cs
<Window x:Class="GolangSetEnvironmentVariables.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:GolangSetEnvironmentVariables" mc:Ignorable="d" Title="Golang Set Environment Variables" Height="151.994" Width="429"> <Grid> <Label x:Name="labelGoRoot" Content="GOROOT : " HorizontalContentAlignment="right" HorizontalAlignment="Left" Margin="14,11,0,0" VerticalAlignment="Top" Width="70"/> <Label x:Name="labelGoPath" Content="GOPATH : " HorizontalContentAlignment="right" HorizontalAlignment="Left" Margin="14,37,0,0" VerticalAlignment="Top" Width="70"/> <TextBox x:Name="textBoxGoRoot" HorizontalAlignment="Left" Height="23" Margin="85,13,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="319"/> <TextBox x:Name="textBoxGoPath" HorizontalAlignment="Left" Height="23" Margin="85,41,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="319"/> <Button x:Name="buttonURL" Content="Linsoo.co.kr" HorizontalAlignment="Left" Margin="14,84,0,0" VerticalAlignment="Top" Width="75" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" BorderThickness="0" Click="buttonURL_Click"/> <Button x:Name="buttonRegist" Content="Regist" HorizontalAlignment="Left" Margin="329,69,0,0" VerticalAlignment="Top" Width="75" Height="35" Click="buttonRegist_Click" /> </Grid> </Window>
MainWindow.xaml
Linsoo
의 저작물인 이 저작물은(는)
크리에이티브 커먼즈 저작자표시-동일조건변경허락 4.0 국제 라이선스
에 따라 이용할 수 있습니다.