public static string ZipFolder(string sourceFolder, string destinationFile) { byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; if (File.Exists(destinationFile)) { File.Delete(destinationFile); } FileStream fs = File.Create(destinationFile); fs.Write(emptyzip, 0, emptyzip.Length); fs.Flush(); fs.Close(); fs = null; File.SetAttributes(destinationFile, FileAttributes.Normal); Shell objShell = new Shell(); Folder destination = objShell.NameSpace(destinationFile); Folder source = objShell.NameSpace(sourceFolder); FolderItems items = source.Items(); destination.CopyHere(items, 20); System.Threading.Thread.Sleep(1000); return destinationFile; }
public static string ExtractZipFile(string sourceFile, string destinationFolder) { Shell objShell = new Shell(); Folder destination = objShell.NameSpace(destinationFolder); Folder source = objShell.NameSpace(sourceFile); foreach (var file in source.Items()) { destination.CopyHere(file, 4 | 16); } return destinationFolder; }
public static void analyseRecycleBin() { try { var _returnList = new List<string>(); var _shell = new Shell(); var countedItemSize = 0; initWorker(); _binWorker.RunWorkerAsync(); _binWorker.DoWork += (o, e) => { var _recBin = _shell.NameSpace(10); // Size of Items in Byte foreach (FolderItem2 item in _recBin.Items()) { countedItemSize += item.Size; } _returnList.Add(_recBin.Items().Count.ToString()); _returnList.Add(countedItemSize.ToString()); _returnList.Add(DateTime.Now.ToString("g")); Marshal.FinalReleaseComObject(_shell); }; _binWorker.RunWorkerCompleted += (o, e) => { if (_stopBinScan != null) { _stopBinScan(_returnList); } }; } catch(Exception ex) { MessageBox.Show(ex.Message, "analyseRecycleBin()"); } }
public static Dictionary<string, string> GetExtendedProperties(string path) { Dictionary<string, string> properties = null; if (!File.Exists(path)) { return properties; } var dirName = Path.GetDirectoryName(path); Shell shell = new Shell(); Folder directory = shell.NameSpace(dirName); FolderItem folderItem = directory.ParseName(Path.GetFileName(path)); Dictionary<int, string> headers = GetExtendedPropertyHeaders(directory); if (folderItem != null) { properties = new Dictionary<string, string>(); for (int i = 0; i < EXTENDED_HEADER_COUNT + 1; i++) { string property = directory.GetDetailsOf(folderItem, i); if (!string.IsNullOrEmpty(property)) { properties.Add(headers[i], property); } } } return properties; }
public static TimeSpan FindLength( string location, int index ) { try { Shell shell = new Shell (); var dr = shell.NameSpace ( location ); var itm = dr.ParseName ( Path.GetFileName ( location ) ); var prop = dr.GetDetailsOf ( itm, index ); return TimeSpan.Parse ( prop ); } catch { return new TimeSpan (); } }
public string GetShortcutTargetFile(string shortcutFilename) { string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); Shell32.Shell shell = new Shell(); Shell32.Folder folder = shell.NameSpace(pathOnly); Shell32.FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; return link.Path; } return string.Empty; }
public static Dictionary<int, string> GetExtendedPropertyHeaders(string path) { Dictionary<int, string> headers = null; var dirName = Path.GetDirectoryName(path); if (!Directory.Exists(dirName)) { return headers; } Shell shell = new Shell(); Folder directory = shell.NameSpace(dirName); headers = GetExtendedPropertyHeaders(directory); return headers; }
static int GetDuration(string filename) { int duration = 0; try { var shl = new Shell(); var fldr = shl.NameSpace(Path.GetDirectoryName(filename)); var itm = fldr.ParseName(Path.GetFileName(filename)); // Index 27 is the video duration [This may not always be the case] var propValue = fldr.GetDetailsOf(itm, 285); duration = Convert.ToInt32(propValue); //return duration; //return TimeSpan.TryParse(propValue, out duration); } catch (Exception) { //Console.WriteLine("Problem 1"); } return duration; }
private bool SaveMusic(Guid mediaCategoryId, IEnumerable<MultipartFileData> fileData) { foreach (MultipartFileData file in fileData) { var repository = factory.GetRepository<Repository<MediaCategory>>(); var query = repository.GetQueryable(); var mediaCategory = query.Where(o => o.Id == mediaCategoryId).FirstOrDefault(); factory.OnTransaction(() => { var actualFileName = file.Headers.ContentDisposition.FileName; var serverFileName = Path.GetFileName(file.LocalFileName); Shell shell = new Shell(); Folder objFolder = shell.NameSpace(ServerTempFolder); FolderItem folderItem = objFolder.ParseName(serverFileName); var resource = new ResourceMusic() { Name = Path.GetFileNameWithoutExtension(Strings.UnquoteToken(actualFileName)), Filename = serverFileName, Location = Path.Combine(ServerUploadFolder, Constants.Music), Description = String.Empty, ResourceType = ResourceType.Music, Extension = Path.GetExtension(serverFileName), Size = objFolder.GetDetailsOf(folderItem, (int)MusicProperties.Size), Artist = objFolder.GetDetailsOf(folderItem, (int)MusicProperties.ContributingArtist), Album = objFolder.GetDetailsOf(folderItem, (int)MusicProperties.Album), Genre = objFolder.GetDetailsOf(folderItem, (int)MusicProperties.Genre), Length = objFolder.GetDetailsOf(folderItem, (int)MusicProperties.Length) }; var destinationPath = Path.Combine(resource.Location, resource.Filename); File.Move(file.LocalFileName, destinationPath); mediaCategory.AddResources(resource); }); } return true; }
/// <summary> ///get icon from lnk file in windows </summary> public static string GetShortcutIcon(string shortcutFilename) { string pathOnly = Os.getDirectoryName(shortcutFilename); string filenameOnly = Os.getFileName(shortcutFilename); Shell shell = new Shell(); Folder folder = shell.NameSpace(pathOnly); FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; string iconlocation = ""; link.GetIconLocation(out iconlocation); return iconlocation; } return string.Empty; }
public void UnZip(string zipFile, string folderPath) { int extractedFiles = 9; int totalFiles = 0; string filename = Path.GetFileName(zipFile); if (!File.Exists(zipFile)) throw new FileNotFoundException(); if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); Shell objShell = new Shell(); Folder destinationFolder = objShell.NameSpace(folderPath); Folder sourceFile = objShell.NameSpace(zipFile); // TODO: this doesn't actually get the number of files in the archive // just the number of items it sees at the top level of the archive // including folders totalFiles = sourceFile.Items().Count; if (StartedUnzip != null) StartedUnzip(this, new StartUnzipEventArgs(filename, 0, totalFiles)); foreach (var file in sourceFile.Items()) { destinationFolder.CopyHere(file, 16); extractedFiles++; // useless unless we use a different method of unzipping. // Shell32 extracts the file in one big step and never gives // us the opportunity to report progress or update the form // from here //if (ProgressedUnzip != null) // ProgressedUnzip(this, new ProgressUnzipEventArgs("extractedfilenamehere", extractedFiles, totalFiles)); } if (EndedUnzip != null) EndedUnzip(this, new EndUnzipEventArgs(zipFile, 0)); }
/// <summary> /// Checks wheter if the shortcut is created. /// </summary> /// <returns></returns> public static bool IsStartupCreated() //If there is a shortcut called WallpaperClock and it's target is current directory. { Shell shell = new Shell(); if (File.Exists(startupPath + "\\WallpaperClock.lnk")) { Folder folder = shell.NameSpace(startupPath); FolderItem folderItem = folder.ParseName("WallpaperClock.lnk"); Shell32.ShellLinkObject link = folderItem.GetLink as Shell32.ShellLinkObject; if (string.Equals(link.Path, currentPath, StringComparison.OrdinalIgnoreCase)) //If the current shortcut's target equals current .exe's location. return true; } return false; }
public void ExtractFilesFromZip(string src, string dest) { Shell sh = new Shell(); Folder inzip = sh.NameSpace(src); Folder outdir = sh.NameSpace(dest); outdir.CopyHere(inzip.Items(), 0); // 4 for no progress dialog, 16 for Yes to all }
/*************************************************************************************************************************/ // SHORTCUTS /// <summary> /// get path from lnk file in windows </summary> public static string[] GetShortcutTargetFile(string shortcutFilename) { try { string pathOnly = Os.getDirectoryName(shortcutFilename); string filenameOnly = Os.getFileName(shortcutFilename); Shell shell = new Shell(); Folder folder = shell.NameSpace(pathOnly); FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; if (link.Arguments != "") { return new string[] { link.Path, link.Arguments }; } return new string[] { link.Path, "" }; } } catch (Exception e) { Program.log.write("GetShortcutTargetFile error: " + e.Message); } return new string[] { "", "" }; }
Dictionary<int, KeyValuePair<string, string>> GetFileProps(string filename) { Shell shl = new Shell(); Folder fldr = shl.NameSpace(Path.GetDirectoryName(filename)); FolderItem itm = fldr.ParseName(Path.GetFileName(filename)); Dictionary<int, KeyValuePair<string, string>> fileProps = new Dictionary<int, KeyValuePair<string, string>>(); for (int i = 0; i < 100; i++) { string propValue = fldr.GetDetailsOf(itm, i); if (propValue != "") { fileProps.Add(i, new KeyValuePair<string, string>(fldr.GetDetailsOf(null, i), propValue)); } } return fileProps; }
/// <summary> /// 日本語名称取得 /// </summary> /// <param name="path"></param> /// <returns></returns> private string GetJpnName(string path) { var dirNm = Path.GetDirectoryName(path); var fileNm = Path.GetFileName(path); var shell = new Shell(); var f = shell.NameSpace(dirNm); var item = f.ParseName(fileNm); return item.Name; }
private static SongDbItems GetSongInfo(string filepath) { SongDbItems songInfo = new SongDbItems(); songInfo.path = filepath; Shell shell = new Shell(); string filedir = Path.GetDirectoryName(filepath); string filename = Path.GetFileName(filepath); Folder objFolder = shell.NameSpace(filedir); if (objFolder != null) { FolderItem fi = objFolder.ParseName(filename); songInfo.name = objFolder.GetDetailsOf(fi, 21); songInfo.album = objFolder.GetDetailsOf(fi, 14); songInfo.artist = objFolder.GetDetailsOf(fi, 20); string year_string = objFolder.GetDetailsOf(fi, 15); if (!String.IsNullOrEmpty(year_string)) { songInfo.year = int.Parse(year_string); } else { songInfo.year = 0; } if (songInfo.name.Contains("'")) { songInfo.name = songInfo.name.Replace("'", "`"); } if (songInfo.album.Contains("'")) { songInfo.album = songInfo.album.Replace("'", "`"); } if (songInfo.artist.Contains("'")) { songInfo.artist = songInfo.artist.Replace("'", "`"); } if (songInfo.path.Contains("'")) { songInfo.path = songInfo.path.Replace("'", "`"); } } return songInfo; }
public string getMovieDuration(string Pathx) { string videoLength; try { var shl = new Shell(); var fldr = shl.NameSpace(Path.GetDirectoryName(Pathx)); var itm = fldr.ParseName(Path.GetFileName(Pathx)); // Index 27 is the video duration [This may not always be the case] var propValue = fldr.GetDetailsOf(itm, 27); // out TimeSpan duration; TimeSpan movieSpan; TimeSpan.TryParse(propValue, out movieSpan); videoLength = movieSpan.ToString("c"); } catch (Exception) { videoLength = ""; } return videoLength; }
public static string GetShortcutTargetFile(string shortcutFilename) { // Get directory string pathOnly = Path.GetDirectoryName(shortcutFilename); // Get filename string filenameOnly = Path.GetFileName(shortcutFilename); Shell shell = new Shell(); Folder folder = shell.NameSpace(pathOnly); FolderItem folderItem = folder.ParseName(filenameOnly); if (folderItem != null) { // Get shortcut target ShellLinkObject link = (ShellLinkObject) folderItem.GetLink; return link.Path; } // Not found, return empty string return ""; }
/// <summary> /// Place installable files in folders /// </summary> /// <param name="workPath"></param> /// <returns></returns> private static bool PrepareBinaries(string workPath) { string WorkBinariesPath = Path.Combine(workPath, BinariesSubDir); bool isCopied = false; if (!IsLocal) { ExtractZip exz = new ExtractZip(); int rc = exz.DownloadAndExtract(GitUrl, workPath, BinariesSubDir, IsX64); isCopied = rc == 0; } else { if (!FolderHelper.MakeFolder(workPath)) return false; if (LocalExePath != WorkBinariesPath) { if (!FolderHelper.MakeFolderReset(WorkBinariesPath)) return false; Shell sh = new Shell(); Folder outdir = sh.NameSpace(WorkBinariesPath); IEnumerable<string> subfiles = Directory.EnumerateFiles(LocalExePath, "*.exe"); foreach (string file in subfiles) { string filepath = Path.Combine(LocalExePath, file); outdir.CopyHere(filepath, 0); // 4 for no progress dialog, 16 for Yes to all } subfiles = Directory.EnumerateFiles(LocalExePath, "*.pdb"); foreach (string file in subfiles) { string filepath = Path.Combine(LocalExePath, file); outdir.CopyHere(filepath, 0); // 4 for no progress dialog, 16 for Yes to all } } // return true if redis-server exists isCopied = File.Exists(Path.Combine(WorkBinariesPath, @"redis-server.exe")); } if (isCopied) { // copy redis.conf file to workPath string redisconf = File.ReadAllText(Path.Combine(RedisConfPath, @"redis.conf")); File.WriteAllText(Path.Combine(workPath, @"redis.conf"), redisconf); } return isCopied; }
/// <summary> /// The main unarchive method. Will overwrite any files that are already there /// </summary> /// <param name="archiveFile">The file to be extracted. Needs to have zip extension</param> /// <param name="unArchiveFolder">The directory to which the file would be extracted</param> /// <returns>True on success, false otherwise</returns> public static bool UnArchive(string archiveFile, string unArchiveFolder) { try { _lastError = ""; // Check if the archive file has correct extension if (!VerifyExtension(archiveFile)) { return false; } Shell shell = new Shell(); Folder archive = shell.NameSpace(Path.GetFullPath(archiveFile)); Folder extractFolder = shell.NameSpace(Path.GetFullPath(unArchiveFolder)); // Copy each item one-by-one foreach (FolderItem f in archive.Items()) { extractFolder.CopyHere(f, 20); } // While the above unarchive procedure works fine, there's // a caveat. The Copying is actually done in a different thread // and there is no direct way to wait while extraction completes. // The following function is a KLUDGE to wait till the item // counts in both source and destination are equal, or a // timeout occurs if (!WaitTillItemCountIsEqual(archive, extractFolder)) { // There was an error waiting for items to be copied // We can directly return from here, the _lastError // has already been set by the function return false; } return true; } catch (Exception ex) { _lastError = "ERROR: Could not unarchive. Exception: " + ex.Message; return false; } }
/// <summary> /// Creates archive from files in given directory /// </summary> /// <param name="archiveFile">The archive file to be created. Needs to have .zip extension</param> /// <param name="unArchiveFolder">The directory from which files/folders will be added to archive</param> /// <returns>True in case of success, false otherwise</returns> public static bool Archive(string archiveFile, string unArchiveFolder) { try { _lastError = ""; // Check if the file has correct extension if (!VerifyExtension(archiveFile)) { return false; } // Create empty archive byte[] emptyArchiveContent = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; FileStream fs = File.Create(Path.GetFullPath(archiveFile)); fs.Write(emptyArchiveContent, 0, emptyArchiveContent.Length); fs.Flush(); fs.Close(); fs = null; Shell shell = new Shell(); Folder archive = shell.NameSpace(Path.GetFullPath(archiveFile)); // Get the archive folder object Folder sourceFolder = shell.NameSpace(Path.GetFullPath(unArchiveFolder)); // Get the source folder object archive.CopyHere(sourceFolder.Items(), 20); // Copy the contents from archive to extract path // While the above archive procedure works fine, there's // a caveat. The Copying is actually done in a different thread // and there is no direct way to wait while archiving completes. // The following function is a KLUDGE to wait till the item // counts in both source and destination are equal, or a // timeout occurs if (!WaitTillItemCountIsEqual(sourceFolder, archive)) { // There was an error waiting for items to be copied // We can directly return from here, the _lastError // has already been set by the function return false; } return true; } catch (Exception ex) { _lastError = "ERROR: Could not create archive. Exception: " + ex.Message; return false; } }