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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsShortcut
{
public class WindowsShortcut
{
private String filename = null;
private bool hasRelativePath = false;
private bool hasDescription = false;
private bool hasWorkingDirectory = false;
private bool hasCommandLineArgument = false;
private bool hasAnIcon = false;
private bool shellItemPresent = false;
private bool fileOnLocalDisk = false;
private int volumeTableOffset = 0;
private int pathOffset = 0;
private int networkTableOffset = 0;
private int finalPathOffset = 0;
private String path = null;
private String finalPath = null;
private String description = null;
private String relativePath = null;
private String workingDirectory = null;
private String arguments = null;
/**
* Initialise l'objet SHORTCUT
* @param filename le fichier .lnk dont on veut récupérer les informations
*/
public WindowsShortcut(String filename)
{
this.filename = filename;
}
/**
* Effectue la lecture du fichier .lnk passé au constructeur
* @throws IOException si le fichier ne peut etre lu
*/
public void processShortcut()
{
// FileStream inStream = new FileStream(new FileStream(filename, FileMode.Open, FileAccess.Read));
FileStream inStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
// Lecture du header
processHeader(inStream);
// Si la liste des items est présente, on s'en occupe
if (shellItemPresent)
{
processShellItem(inStream);
}
// Lecture des informations sur la location du fichier
processFileLocationInfo(inStream);
// Si le fichier est en local, on traite la table de volume local
if (fileOnLocalDisk)
{
processLocalVolumeTable(inStream);
}
// Sinon, on s'occupe de celle réseau
else
{
processNetworkVolumeTable(inStream);
}
// On traite le chemin de base
processPath(inStream);
// On traite le chemin final
processfinalPath(inStream);
// Si le raccourci a une description, on s'en occupe
if (hasDescription)
{
description = processString(inStream);
}
// Si le raccourci a un chemin relatif, on s'en occupe
if (hasRelativePath)
{
relativePath = processString(inStream);
}
// Si le raccourci a un répertoire de travail, on s'en occupe
if (hasWorkingDirectory)
{
workingDirectory = processString(inStream);
// Cas particulier pour traiter le répertoire de travail c:\
if (workingDirectory == ("%HOMEDRIVE%%HOMEPATH%"))
{
workingDirectory = "C:\\";
}
}
// Si il y a des arguments, on les récupère
if (hasCommandLineArgument)
{
arguments = processString(inStream);
}
// Fermeture du flux vers le fichier .lnk
inStream.Close();
}
private String processString(FileStream inStream)
{
int length = mYReadShort(inStream);
//StringBuffer buf = new StringBuffer(length);
String buf = "";
byte temp = 1;
for (int i = 0; i < length; i++)
{
buf = buf + ((char)(mYReadShort(inStream)));
}
return buf;
}
private void processfinalPath(FileStream inStream)
{
//StringBuffer buf = new StringBuffer();
String buf = "";
byte temp = 1;
//while ((temp = inStream.readByte()) != 0)
while ((temp = (byte)inStream.ReadByte()) != 0)
{
//buf.append((char)temp);
buf = buf + ((char)temp);
}
finalPath = buf;
}
private void processPath(FileStream inStream)
{
//StringBuffer buf = new StringBuffer();
String buf = "";
byte temp = 1;
//while ((temp = inStream.readByte()) != 0)
while ((temp = (byte)inStream.ReadByte()) != 0)
{
//buf.append((char)temp);
buf = buf + ((char)temp);
}
path = buf;
}
private void processLocalVolumeTable(FileStream inStream)
{
int structureLocalVolumeLenght = mYReadInt(inStream);
int driveType = mYReadInt(inStream); ;
// Volume Serial Number
//inStream.readInt();
inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte();
// Volume name offset (always 0x10)
//inStream.readInt();
inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte();
inStream.Seek(structureLocalVolumeLenght - 16, SeekOrigin.Current);
//inStream.skipBytes(structureLocalVolumeLenght - 16);
}
private void processNetworkVolumeTable(FileStream inStream)
{
int structureLocalVolumeLenght = mYReadInt(inStream);
int driveType = mYReadInt(inStream);
// Volume Serial Number
//inStream.readInt();
inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte();
// Volume name offset (always 0x10)
//inStream.readInt();
inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte(); inStream.ReadByte();
//inStream.skipBytes(structureLocalVolumeLenght - 16);
inStream.Seek(structureLocalVolumeLenght - 16, SeekOrigin.Current);
}
private void processFileLocationInfo(FileStream inStream)
{
int dataStructureInfo = mYReadInt(inStream); ;
int dataStructureEndOffset = mYReadInt(inStream); ;
fileOnLocalDisk = (mYReadInt(inStream) == 1 ? true : false);
volumeTableOffset = mYReadInt(inStream);
pathOffset = mYReadInt(inStream);
networkTableOffset = mYReadInt(inStream);
finalPathOffset = mYReadInt(inStream);
}
private void processHeader(FileStream inStream)
{
//read first 4 bytes, can test if they are 4C 00 00 00
mYReadInt(inStream);
// GUID 16 bytes, current is 01 14 02 00 00 00 00 00 C0 00 00 00 00 00 46
mYReadLong(inStream);
mYReadLong(inStream);
// read shortcut flags 1 dword (double word -> 4 bytes -> 1 Int)
processFlag(mYReadInt(inStream));
// read target file flags 1 dword (idem)
mYReadInt(inStream);
// Read dates creation, modification, last access
mYReadLong(inStream);
mYReadLong(inStream);
mYReadLong(inStream);
// Read the length of the file
mYReadInt(inStream);
// Read the index of the icon, 0 if no custom icon
mYReadInt(inStream);
// Read the state of the program windows (1 : normal, 2 mini, 3 max)
mYReadInt(inStream);
// Read the Hot Key
mYReadInt(inStream);
// Read the 2 last dword of the header
mYReadLong(inStream);
}
private void processShellItem(FileStream inStream)
{
int itemListLength = mYReadShort(inStream);
//inStream.skipBytes(itemListLength);
inStream.Seek(itemListLength, SeekOrigin.Current);
}
private void processFlag(int flag)
{
if ((flag & 1) == 1)
{
shellItemPresent = true;
}
if ((flag & 4) == 4)
{
hasDescription = true;
}
if ((flag & 8) == 8)
{
hasRelativePath = true;
}
if ((flag & 16) == 16)
{
hasWorkingDirectory = true;
}
if ((flag & 32) == 32)
{
hasCommandLineArgument = true;
}
if ((flag & 64) == 64)
{
hasAnIcon = true;
}
}
public String ToString()
{
return "Raccourci du fichier " + filename + " : \n" + "Une description : " +
(hasDescription ? "OUI\n" : "NON\n") +
"Un répertoire de travail : " +
(hasWorkingDirectory ? "OUI\n" : "NON\n") +
"Des arguments : " + (hasCommandLineArgument ? "OUI\n" : "NON\n") +
"Une chemin relatif : " + (hasRelativePath ? "OUI\n" : "NON\n") +
"une icone : " + (hasAnIcon ? "OUI\n" : "NON\n") +
"Item : " + (shellItemPresent ? "OUI\n" : "NON\n") +
"LE CHEMIN D'ACCES : " + path +
"\nLE CHEMIN FINAL : " + finalPath +
"\nLA DESCRIPTION : " + description +
"\nCHEMIN RELATIF : " + relativePath +
"\nREPERTOIRE DE TRAVAIL : " + workingDirectory +
"\nARGUMENTS : " + arguments;
}
public String getWorkingDirectory()
{
return workingDirectory;
}
public String getPath()
{
return path;
}
public String getDescription()
{
return description;
}
public String getArguments()
{
return arguments;
}
private int mYReadInt(FileStream inStream)
{
byte a = (byte)inStream.ReadByte();
byte b = (byte)inStream.ReadByte();
byte c = (byte)inStream.ReadByte();
byte d = (byte)inStream.ReadByte();
return ((d & 0xFF) << 24) | ((c & 0xFF) << 16) | ((b & 0xFF) << 8) |
(a & 0xFF);
}
private int mYReadShort(FileStream inStream)
{
byte a = (byte)inStream.ReadByte();
byte b = (byte)inStream.ReadByte();
int ret = 0;
return ret | ((b & 0xFF) << 8) | (a & 0xFF);
}
private long mYReadLong(FileStream inStream)
{
byte a = (byte)inStream.ReadByte();
byte b = (byte)inStream.ReadByte();
byte c = (byte)inStream.ReadByte();
byte d = (byte)inStream.ReadByte();
byte e = (byte)inStream.ReadByte();
byte f = (byte)inStream.ReadByte();
byte g = (byte)inStream.ReadByte();
byte h = (byte)inStream.ReadByte();
return ((h & 0xFF) << 56) | ((g & 0xFF) << 48) | ((f & 0xFF) << 40) |
((e & 0xFF) << 32) |
((d & 0xFF) << 24) | ((c & 0xFF) << 16) | ((b & 0xFF) << 8) |
(a & 0xFF);
}
public static void Main(String[] args)
{
//WindowsShortcut test = new WindowsShortcut("C:\\temp\\l.lnk");
WindowsShortcut test = new WindowsShortcut("C:\\temp\\temp.lnk");
try
{
test.processShortcut();
System.Console.WriteLine(test.ToString());
}
catch (IOException e)
{
System.Console.WriteLine(e.ToString());
}
}
}
} |
Partager