LinkToolAddin/ui/mcp/McpConfigWindow.xaml.cs

72 lines
2.5 KiB
C#

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;
using LinkToolAddin.host;
using LinkToolAddin.host.mcp;
using Newtonsoft.Json;
using Path = System.IO.Path;
namespace LinkToolAddin.ui.mcp
{
/// <summary>
/// Interaction logic for McpConfigWindow.xaml
/// </summary>
public partial class McpConfigWindow : ArcGIS.Desktop.Framework.Controls.ProWindow
{
private static Dictionary<string,McpServer> servers = new Dictionary<string, McpServer>();
public McpConfigWindow()
{
InitializeComponent();
string mcpConfigStr = ReadMcpConfig();
McpJsonTextBox.Text = mcpConfigStr;
}
private string ReadMcpConfig()
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string configDirPath = Path.Combine(appDataPath, "LinkTool");
string mcpConfigPath = Path.Combine(appDataPath, "LinkTool", "McpConfig.json");
Directory.CreateDirectory(configDirPath);
if (!File.Exists(mcpConfigPath))
{
File.Create(mcpConfigPath);
McpServerList mcpServerList = new McpServerList();
string json = JsonConvert.SerializeObject(mcpServerList.GetAllServers());
File.WriteAllText(mcpConfigPath, json);
return json;
}
else
{
string mcpConfigStr = File.ReadAllText(mcpConfigPath);
McpServerList mcpServerList = new McpServerList(mcpConfigStr);
return JsonConvert.SerializeObject(mcpServerList.GetAllServers());
}
}
private void WriteMcpConfig(string json)
{
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string configDirPath = Path.Combine(appDataPath, "LinkTool");
string mcpConfigPath = Path.Combine(appDataPath, "LinkTool", "McpConfig.json");
Directory.CreateDirectory(configDirPath);
if (!File.Exists(mcpConfigPath))
{
File.Create(mcpConfigPath);
}
File.WriteAllText(mcpConfigPath, json);
}
}
}