提取方法
This commit is contained in:
@@ -5,6 +5,7 @@ using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
/// <summary>
|
||||
/// 提取一些代码
|
||||
@@ -45,7 +46,6 @@ namespace GeekDesk.Util
|
||||
/// <param name="appData"></param>
|
||||
public static void SaveAppData(AppData appData)
|
||||
{
|
||||
|
||||
using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Create))
|
||||
{
|
||||
BinaryFormatter bf = new BinaryFormatter();
|
||||
@@ -74,6 +74,100 @@ namespace GeekDesk.Util
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据路径获取文件图标等信息
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static IconInfo GetIconInfoByPath(string path)
|
||||
{
|
||||
string tempPath = path;
|
||||
|
||||
//string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
|
||||
//string ext = "";
|
||||
//if (!ImageUtil.IsSystemItem(path))
|
||||
//{
|
||||
// ext = System.IO.Path.GetExtension(path).ToLower();
|
||||
//}
|
||||
|
||||
string iconPath = null;
|
||||
//if (".lnk".Equals(ext))
|
||||
//{
|
||||
|
||||
string targetPath = FileUtil.GetTargetPathByLnk(path);
|
||||
iconPath = FileUtil.GetIconPathByLnk(path);
|
||||
if (targetPath != null)
|
||||
{
|
||||
path = targetPath;
|
||||
}
|
||||
//}
|
||||
if (StringUtil.IsEmpty(iconPath))
|
||||
{
|
||||
iconPath = path;
|
||||
}
|
||||
|
||||
BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
|
||||
IconInfo iconInfo = new IconInfo
|
||||
{
|
||||
Path = path,
|
||||
LnkPath = tempPath,
|
||||
BitmapImage = bi,
|
||||
StartArg = FileUtil.GetArgByLnk(tempPath)
|
||||
};
|
||||
iconInfo.DefaultImage = iconInfo.ImageByteArr;
|
||||
iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
|
||||
if (StringUtil.IsEmpty(iconInfo.Name))
|
||||
{
|
||||
iconInfo.Name = path;
|
||||
}
|
||||
return iconInfo;
|
||||
}
|
||||
|
||||
|
||||
public static IconInfo GetIconInfoByPath_NoWrite(string path)
|
||||
{
|
||||
string tempPath = path;
|
||||
|
||||
//string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
|
||||
string ext = "";
|
||||
if (!ImageUtil.IsSystemItem(path))
|
||||
{
|
||||
ext = System.IO.Path.GetExtension(path).ToLower();
|
||||
}
|
||||
|
||||
string iconPath = null;
|
||||
if (".lnk".Equals(ext))
|
||||
{
|
||||
|
||||
string targetPath = FileUtil.GetTargetPathByLnk(path);
|
||||
iconPath = FileUtil.GetIconPathByLnk(path);
|
||||
if (targetPath != null)
|
||||
{
|
||||
path = targetPath;
|
||||
}
|
||||
}
|
||||
if (StringUtil.IsEmpty(iconPath))
|
||||
{
|
||||
iconPath = path;
|
||||
}
|
||||
|
||||
BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
|
||||
IconInfo iconInfo = new IconInfo
|
||||
{
|
||||
Path_NoWrite = path,
|
||||
LnkPath_NoWrite = tempPath,
|
||||
BitmapImage_NoWrite = bi,
|
||||
StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
|
||||
};
|
||||
iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
|
||||
iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
|
||||
if (StringUtil.IsEmpty(iconInfo.Name))
|
||||
{
|
||||
iconInfo.Name_NoWrite = path;
|
||||
}
|
||||
return iconInfo;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct RECT
|
||||
|
||||
137
Util/FileUtil.cs
137
Util/FileUtil.cs
@@ -2,7 +2,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GeekDesk.Util
|
||||
@@ -10,25 +12,152 @@ namespace GeekDesk.Util
|
||||
public class FileUtil
|
||||
{
|
||||
|
||||
private static readonly string NO_PATH = "{(.*)}";
|
||||
private static readonly string NO_ICO = "^,(.*)";
|
||||
private static readonly string HAVE_ICO = "(.*),(.*)";
|
||||
|
||||
public static string GetTargetPathByLnk(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
WshShell shell = new WshShell();
|
||||
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
|
||||
|
||||
if (StringUtil.IsEmpty(shortcut.TargetPath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return shortcut.TargetPath;
|
||||
string path = shortcut.TargetPath;
|
||||
if (path == null || Regex.IsMatch(path, NO_PATH))
|
||||
{
|
||||
path = ParseShortcut(filePath);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
#pragma warning disable CS0168 // 声明了变量“e”,但从未使用过
|
||||
catch (Exception e)
|
||||
#pragma warning restore CS0168 // 声明了变量“e”,但从未使用过
|
||||
{
|
||||
LogUtil.WriteErrorLog(e, "获取目标路径失败! filePath=" + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取启动参数
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetArgByLnk(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
WshShell shell = new WshShell();
|
||||
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
|
||||
return shortcut.Arguments;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogUtil.WriteErrorLog(e, "获取启动参数失败! filePath=" + filePath);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取iconpath
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetIconPathByLnk(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
WshShell shell = new WshShell();
|
||||
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
|
||||
var iconPath = shortcut.IconLocation;
|
||||
|
||||
if (StringUtil.IsEmpty(iconPath)
|
||||
|| Regex.IsMatch(iconPath, NO_ICO)
|
||||
|| Regex.IsMatch(iconPath, NO_PATH)
|
||||
|| !Regex.IsMatch(iconPath, HAVE_ICO))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return iconPath.Split(',')[0];
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogUtil.WriteErrorLog(e, "获取图标路径失败! filePath=" + filePath);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
UINT MsiGetShortcutTarget(
|
||||
LPCTSTR szShortcutTarget,
|
||||
LPTSTR szProductCode,
|
||||
LPTSTR szFeatureId,
|
||||
LPTSTR szComponentCode
|
||||
);
|
||||
*/
|
||||
[DllImport("msi.dll", CharSet = CharSet.Auto)]
|
||||
static extern int MsiGetShortcutTarget(string targetFile, StringBuilder productCode, StringBuilder featureID, StringBuilder componentCode);
|
||||
|
||||
public enum InstallState
|
||||
{
|
||||
NotUsed = -7,
|
||||
BadConfig = -6,
|
||||
Incomplete = -5,
|
||||
SourceAbsent = -4,
|
||||
MoreData = -3,
|
||||
InvalidArg = -2,
|
||||
Unknown = -1,
|
||||
Broken = 0,
|
||||
Advertised = 1,
|
||||
Removed = 1,
|
||||
Absent = 2,
|
||||
Local = 3,
|
||||
Source = 4,
|
||||
Default = 5
|
||||
}
|
||||
|
||||
public const int MaxFeatureLength = 38;
|
||||
public const int MaxGuidLength = 38;
|
||||
public const int MaxPathLength = 1024;
|
||||
|
||||
/*
|
||||
INSTALLSTATE MsiGetComponentPath(
|
||||
LPCTSTR szProduct,
|
||||
LPCTSTR szComponent,
|
||||
LPTSTR lpPathBuf,
|
||||
DWORD* pcchBuf
|
||||
);
|
||||
*/
|
||||
[DllImport("msi.dll", CharSet = CharSet.Auto)]
|
||||
static extern InstallState MsiGetComponentPath(string productCode, string componentCode, StringBuilder componentPath, ref int componentPathBufferSize);
|
||||
|
||||
public static string ParseShortcut(string file)
|
||||
{
|
||||
StringBuilder product = new StringBuilder(MaxGuidLength + 1);
|
||||
StringBuilder feature = new StringBuilder(MaxFeatureLength + 1);
|
||||
StringBuilder component = new StringBuilder(MaxGuidLength + 1);
|
||||
|
||||
MsiGetShortcutTarget(file, product, feature, component);
|
||||
|
||||
int pathLength = MaxPathLength;
|
||||
StringBuilder path = new StringBuilder(pathLength);
|
||||
|
||||
InstallState installState = MsiGetComponentPath(product.ToString(), component.ToString(), path, ref pathLength);
|
||||
if (installState == InstallState.Local)
|
||||
{
|
||||
return path.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user