2 Star 9 Fork 4

Talon/ScriptBox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
TalonsWorkSpace.cs 4.65 KB
一键复制 编辑 原始数据 按行查看 历史
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Talon
{
class ProjectInfo
{
public string name { get; set; }
public string path { get; set; }
}
class WorkspaceConfig
{
public string currentProject { get; set; }
public List<ProjectInfo> recentProjects { get; set; }
}
class TalonsWorkSpace
{
public string CurrentProjectPath { get; set; }
public string CurrentProjectDir { get; set; }
public WorkspaceConfig Config { get; set; }
public static string WorkspaceConfigFile { get; set; }
public event EventHandler ReloadMenuEvent;//更新菜单事件
public TalonsWorkSpace(string workspacePath)
{
WorkspaceConfigFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), string.Format("{0}.json",workspacePath));
}
/// <summary>
/// 加载工作空间json文件
/// </summary>
/// <returns></returns>
public string Load()
{
// 判断文件是否存在
if (!File.Exists(WorkspaceConfigFile))
{
// 创建文件并写入一个空的JSON对象
using (StreamWriter sw = File.CreateText(WorkspaceConfigFile))
{
sw.Write("{}");
}
return null;
}
// 读取config.json文件
string jsonString = File.ReadAllText(WorkspaceConfigFile);
try
{
// 解析 JSON
Config = JsonConvert.DeserializeObject<WorkspaceConfig>(jsonString);
if (Config.currentProject == null) return null;
foreach (var item in Config.recentProjects)
{
if(item.name == Config.currentProject)
{
CurrentProjectPath = item.path;
}
}
ReloadMenuEvent(this, EventArgs.Empty);
return CurrentProjectPath;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 清除最近项目
/// </summary>
public void ClearRecentProject()
{
Config.recentProjects.Clear();
SetCurrentProject(Config.currentProject, CurrentProjectPath);//清空后此处会刷新最近项目
// 将MyJsonObject对象序列化为JSON字符串
string json = JsonConvert.SerializeObject(Config, Formatting.Indented);
File.WriteAllText(WorkspaceConfigFile, json);
}
/// <summary>
/// 修改当前项目
/// </summary>
/// <param name="projectName"></param>
/// <param name="projectPath"></param>
public void SetCurrentProject(string projectName, string projectPath)
{
Config.currentProject = projectName;
CurrentProjectPath = projectPath;
CurrentProjectDir = Path.GetDirectoryName(projectPath);
if (!IsProjcetIsRecent(projectName))
{
Config.recentProjects.Add(new ProjectInfo { name = projectName, path = projectPath });
ReloadMenuEvent(this, EventArgs.Empty);//当打开的项目不是最近项目时才会刷新以新增
}
else
{
EditProjectPath(projectName, projectPath);
}
// 将MyJsonObject对象序列化为JSON字符串
string json = JsonConvert.SerializeObject(Config, Formatting.Indented);
File.WriteAllText(WorkspaceConfigFile, json);
}
void EditProjectPath(string projectName, string projectPath)
{
if (Config.recentProjects == null)
{
Config.recentProjects = new List<ProjectInfo>();
}
foreach (var item in Config.recentProjects)
{
if (item.name == projectName)
{
item.path = projectPath;
}
}
}
bool IsProjcetIsRecent(string projectName)
{
if(Config.recentProjects == null)
{
Config.recentProjects = new List<ProjectInfo>();
}
foreach (var item in Config.recentProjects)
{
if (item.name == projectName)
{
return true;
}
}
return false;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/talonshaw/ScriptBox.git
git@gitee.com:talonshaw/ScriptBox.git
talonshaw
ScriptBox
ScriptBox
master

搜索帮助