珠海网站建设排名,国产比较好的精华,wordpress怎么做404页面跳转,写作网站可保存发现网上一键切换JDK环境的方法都是在mac或Linux下的#xff0c;本人主力电脑是Windows#xff0c;于是看了一下WPF的文档#xff0c;自己开发了一个客户端。
直接上代码吧#xff1a;
using JavaSwitch.Properties;
using Newtonsoft.Json;
using System;
using System.…发现网上一键切换JDK环境的方法都是在mac或Linux下的本人主力电脑是Windows于是看了一下WPF的文档自己开发了一个客户端。
直接上代码吧
using JavaSwitch.Properties;
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Forms;namespace JavaSwitch
{public class ListItem{public string Text { get; set; }public bool IsSelected { get; set; }public ListItem(string text){Text text;}}/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public ObservableCollectionListItem Items { get; set; }public MainWindow(){InitializeComponent();// 从本地加载已有的数据LoadDataFromJson();// 数据绑定上下文DataContext this;}private void Button_Add(object sender, RoutedEventArgs e){FolderBrowserDialog folderBrowserDialog new FolderBrowserDialog{// 设置初始目录RootFolder Environment.SpecialFolder.Desktop};// 这个方法可以显示文件夹选择对话框folderBrowserDialog.ShowDialog();// 获取选择的文件夹的全路径名string directoryPath folderBrowserDialog.SelectedPath;// 打印出选择的路径Console.WriteLine(directoryPath);if (string.IsNullOrEmpty(directoryPath)){return;}// 持久化到本地var newItem new ListItem(directoryPath);Items.Add(newItem);SaveDataToJson();}/// summary/// 删除一项/// /summaryprivate void Button_Delete(object sender, RoutedEventArgs e){var deleteButton sender as System.Windows.Controls.Button;if (deleteButton ! null){var listItem deleteButton.DataContext as ListItem;if (listItem ! null){Items.Remove(listItem);SaveDataToJson();}}}private void Button_Click_1(object sender, RoutedEventArgs e){// 打开环境变量设置页面 rundll32 sysdm.cpl,EditEnvironmentVariablesProcess.Start(rundll32.exe, sysdm.cpl,EditEnvironmentVariables);//string originalPath Environment.GetEnvironmentVariable(PATH, EnvironmentVariableTarget.Machine);//System.Windows.MessageBox.Show(Original Path: originalPath);}private void RadioButton_Click(object sender, RoutedEventArgs e){var radioButton sender as System.Windows.Controls.RadioButton;if (radioButton ! null){var listItem radioButton.DataContext as ListItem;if (listItem ! null){foreach (var item in Items){item.IsSelected item listItem;if (item.IsSelected){// 设置环境变量// 执行bash命令setx JAVA_PATH %JAVA_HOME%\bin;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; /MProcess.Start(cmd, /c setx JAVA_PATH \ item.Text \\bin; item.Text \\lib\\dt.jar; item.Text \\lib\\tools.jar;\ /M);// 执行bash命令refreshenv.cmdProcess.Start(cmd, /c refreshenv.cmd);}}}}SaveDataToJson();}private string ENV_DATA JavaEnvironment.json;/// summary/// 保存数据到本地/// /summaryprivate void SaveDataToJson(){var json JsonConvert.SerializeObject(Items, Formatting.Indented);File.WriteAllText(ENV_DATA, json);}/// summary/// 从本地加载数据/// /summaryprivate void LoadDataFromJson(){if (File.Exists(ENV_DATA)){var json File.ReadAllText(ENV_DATA);Items JsonConvert.DeserializeObjectObservableCollectionListItem(json);}else{Items new ObservableCollectionListItem();}}}
}
布局文件
Window x:ClassJavaSwitch.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:JavaSwitchmc:IgnorabledTitleJava环境切换 Height450 Width800GridTextBlock FontSize18 FontWeightBold Foreground#ffbe4d4d HorizontalAlignmentLeft Margin10,10,0,0 TextWrappingWrap Text请用管理员身份打开本程序否则没有权限修改设置 VerticalAlignmentTop Width586/TextBlock FontSize16 HorizontalAlignmentLeft Margin10,47,0,0 TextWrappingWrap Text第一步打开系统变量-Path检查并添加%JAVA_PATH%已添加则不用重复添加 VerticalAlignmentTop Width629/TextBlock FontSize16 HorizontalAlignmentLeft Margin10,84,0,0 TextWrappingWrap Text第二步添加已安装的JDK文件夹 VerticalAlignmentTop Width263/Button FontSize16 Content检查环境变量 HorizontalAlignmentLeft Margin639,47,0,0 VerticalAlignmentTop ClickButton_Click_1/Button FontSize16 Content添加一个Java环境 HorizontalAlignmentLeft Margin257,82,0,0 VerticalAlignmentTop ClickButton_Add/TextBlock FontSize16 HorizontalAlignmentLeft Margin10,123,0,0 TextWrappingWrap Text第三步点击按钮切换环境 VerticalAlignmentTop/ListBox x:NamelistBox ItemsSource{Binding Items} Margin0,156,0,0ListBox.ItemTemplateDataTemplateStackPanel OrientationHorizontal Margin0, 10, 0, 10Viewbox Height22 VerticalAlignmentCenterRadioButton x:NameradioButton GroupNameRadioGroup IsChecked{Binding IsSelected, ModeTwoWay} ClickRadioButton_Click //ViewboxTextBlock Text{Binding Text} VerticalAlignmentCenter FontSize16 Margin10,0,0,0/Button Content删除 VerticalAlignmentCenter ClickButton_Delete FontSize16 Margin20,0,0,0//StackPanel/DataTemplate/ListBox.ItemTemplate/ListBox/Grid
/Window
github链接https://github.com/ITAnt/JavaSwitch 安装文件在根目录的release文件夹