-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZone1.cs
104 lines (88 loc) · 3.59 KB
/
Zone1.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WordAddIns
{
public partial class Zone1
{
private void Zone1_Load(object sender, RibbonUIEventArgs e)
{
}
public static string ConvertOneDriveUrlToLocalPath(string url)
{
// 替换为你的 OneDrive 本地文件夹路径
string onedriveLocalPath = Environment.ExpandEnvironmentVariables(@"C:\Users\qianb\OneDrive");
try
{
// 提取 URL 中的相对路径
Uri uri = new Uri(url);
string relativePath = uri.AbsolutePath.Substring(uri.AbsolutePath.IndexOf("/d.docs.live.net/") + "/d.docs.live.net/".Length);
relativePath = relativePath.Substring(relativePath.IndexOf("/") + 1);
// 解码 URL 中的特殊字符
relativePath = Uri.UnescapeDataString(relativePath);
// 构建本地路径
string localPath = Path.Combine(onedriveLocalPath, relativePath.Replace("/", Path.DirectorySeparatorChar.ToString()));
// 规范化路径以使用正确的分隔符
localPath = Path.GetFullPath(localPath);
return localPath;
}
catch (Exception ex)
{
MessageBox.Show($"Error: Unable to parse OneDrive URL. {ex.Message}");
return null;
}
}
private string GetActiveDocumentPath()
{
// 获取当前活动文档的完整路径
var activeDocument = Globals.ThisAddIn.Application.ActiveDocument;
if (activeDocument != null)
{
return activeDocument.FullName;
}
return null;
}
private void OpenInFolder_Click(object sender, RibbonControlEventArgs e)
{
// 获取当前活动文档的路径
string activeDocumentPath = GetActiveDocumentPath();
// 判断是否需要对OneDrive URL 进行转换
if (activeDocumentPath.Contains("d.docs.live.net"))
{
activeDocumentPath = ConvertOneDriveUrlToLocalPath(activeDocumentPath);
}
if (!string.IsNullOrEmpty(activeDocumentPath))
{
// 获取文件所在的目录
string directoryPath = Path.GetDirectoryName(activeDocumentPath);
if (Directory.Exists(directoryPath))
{
// 打开文件所在的目录
Process.Start("explorer.exe", $"/select,\"{activeDocumentPath}\"");
//System.Diagnostics.Process.Start("explorer.exe", "/select," + activeDocumentPath);
/*var startInfo = new ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = $"/select,\"{activeDocumentPath}\"",
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Normal
};
Process.Start(startInfo);*/
}
else
{
MessageBox.Show("目录不存在。");
}
}
else
{
MessageBox.Show("未找到活动文档。");
}
}
}
}