public static IEnumerable<string> Execute(string fileArchive, string directoryArchive, string executableName) { var failed = new List<string>(); var downloadFolder = new KnownFolder(KnownFolderType.Downloads); var downloads = downloadFolder.Path; //Move old directory to a new old directory var old = Path.Combine(downloads, fileArchive); var dir = Path.Combine(downloads, directoryArchive); if (Directory.Exists(old)) { CreateNestedOld(fileArchive, downloads, old); } else { Directory.CreateDirectory(old); } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } failed.AddRange(ArchiveDirectories(fileArchive, directoryArchive, downloads, dir)); failed.AddRange(ArchiveFiles(executableName, downloads, old)); return failed; }
public static string GetFolderPath(KnownFolder folder) { var ret = new StringBuilder(260); try { var output = SHGetSpecialFolderPath(IntPtr.Zero, ret, folder); if (!output) { return null; } } catch /* (Exception e) */ { return null; } return ret.ToString(); }
private static string GetPath(KnownFolder knownFolder, KnownFolderFlags flags, bool defaultUser) { IntPtr outPath; int result = SHGetKnownFolderPath(new Guid(_knownFolderGuids[(int)knownFolder]), (uint)flags, new IntPtr(defaultUser ? -1 : 0), out outPath); if (result >= 0) { return Marshal.PtrToStringUni(outPath); } else { throw new ExternalException("Unable to retrieve the known folder path. It may not " + "be available on this system.", result); } }
public static string GetKnownFolder(KnownFolder folder) { Guid id; switch (folder) { case KnownFolder.Downloads: default: id = FOLDERID_Downloads; break; } IntPtr path_ptr; SHGetKnownFolderPath(ref id, 0, IntPtr.Zero, out path_ptr); var path = Marshal.PtrToStringUni(path_ptr); Marshal.FreeCoTaskMem(path_ptr); return path; }
public DirectoryInfoEx(KnownFolderIds knownFolderId) : this(KnownFolder.FromKnownFolderId( EnumAttributeUtils <KnownFolderGuidAttribute, KnownFolderIds> .FindAttribute(knownFolderId).Guid)) { }
// ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- /// <summary> /// Gets the current path to the specified known folder as currently configured. This does not require the /// folder to be existent. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <returns>The default path of the known folder.</returns> /// <exception cref="ExternalException">Thrown if the path could not be retrieved.</exception> public static string GetPath(KnownFolder knownFolder) { return(GetPath(knownFolder, false)); }
/// <summary> /// Creates and initializes the known folder. /// </summary> /// <param name="knownFolder">The known folder which will be initialized.</param> /// <param name="defaultUser">Specifies if the paths of the default user (user profile /// template) will be used. This requires administrative rights.</param> /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the known /// folder could not be initialized.</exception> public static void Initialize(KnownFolder knownFolder, bool defaultUser) { GetPath(knownFolder, KnownFolderFlags.Create | KnownFolderFlags.Init, defaultUser); }
/// <summary> /// Downloads report, saves to disk, and extracts CSV file. Lots of disk I/O. /// This method is retained for historic reasons. /// </summary> /// <param name="report"></param> /// <returns></returns> public string ExtractCsvFileForReportToDisk(Report report) { if ((report == null) || (report.Url == null) || (report.Url == string.Empty) || (report.FileName == null) || (report.FileName == string.Empty)) { throw new ArgumentException("Report object missing Url or Filename fields."); } KnownFolder knownFolder = new KnownFolder(KnownFolderType.Downloads); FileStream fs = null; ZipFile zipFile = null; string csv_file_contents = string.Empty; FileStream fs1 = null; StreamReader reader = null; string fullZipToPath = string.Empty; try { // download the zip file and save it in user's Downloads folder using (var client = new WebClient()) { client.DownloadFile(report.Url, knownFolder.Path + @"\" + report.FileName); } fs = File.OpenRead(knownFolder.Path + @"\" + report.FileName); zipFile = new ZipFile(fs); foreach (ZipEntry zipEntry in zipFile) { string entryFileName = zipEntry.Name; byte[] buffer = new byte[4096]; Stream zipStream = zipFile.GetInputStream(zipEntry); fullZipToPath = Path.Combine(knownFolder.Path, entryFileName); using (FileStream streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); streamWriter.Close(); } fs1 = File.OpenRead(fullZipToPath); reader = new StreamReader(fs1); reader = new StreamReader(zipStream); csv_file_contents = reader.ReadToEnd(); } } catch (Exception e) { LogError(e.ToString()); throw new Exception("Problem extracting csv from zip file!", e); } finally { if (zipFile != null) { zipFile.IsStreamOwner = true; zipFile.Close(); } if (fs1 != null) { fs1.Close(); reader.Close(); } } LogDebug(csv_file_contents); return(csv_file_contents); }
/// <summary> /// Gets the current path to the specified known folder as currently configured. This does /// not require the folder to exist. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <returns>The default path of the known folder, or an empty string if the path couldn't be retrieved.</returns> public static string GetPath(KnownFolder knownFolder) { return(GetPath(knownFolder, NativeMethods.KnownFolderFlags.DontVerify)); }
/// <summary> /// Creates and initializes the known folder. /// </summary> /// <param name="knownFolder">The known folder which will be initialized.</param> /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the known /// folder could not be initialized.</exception> public static void Initialize(KnownFolder knownFolder) { Initialize(knownFolder, false); }
/// <summary> /// Gets the current path to the specified known folder as currently configured. This does /// not require the folder to be existent. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <returns>The default path of the known folder.</returns> /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path /// could not be retrieved.</exception> public static string GetPath(KnownFolder knownFolder) { return GetPath(knownFolder, false); }
/// <summary>Creates a <see cref="DirectoryInfo"/> from a given <see cref="KnownFolder"/>. </summary> /// <param name="KnownFolder">The known folder.</param> /// <returns><see cref="DirectoryInfo"/></returns> public static DirectoryInfo GetDirectoryInfo(this KnownFolder KnownFolder) => TryParseDirectoryInfo(KnownFolder.Path, out DirectoryInfo DirectoryInfo) ? DirectoryInfo : null;
public static bool TryGetKnownFolderType(this DirectoryInfoEx directoryInfoEx, out KnownFolder knownFolder) { try { knownFolder = directoryInfoEx.KnownFolderType; return(knownFolder != null); } catch (Exception) { knownFolder = null; return(false); } }
/// <summary> /// Creates and returns a <see cref="StoragePath"/> instance which locates a specific /// folder which is typically present in a file system. /// </summary> /// <param name="knownFolder"> /// A folder which is typically present in a file system. /// </param> /// <returns> /// A new <see cref="StoragePath"/> instance which locates the folder represented by /// the <paramref name="knownFolder"/> parameter. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="knownFolder"/> is an invalid <see cref="KnownFolder"/> enumeration value. /// </exception> /// <exception cref="NotSupportedException"> /// The requested folder is not supported by this file system implementation. /// </exception> /// <seealso cref="TryGetPath(KnownFolder, out StoragePath?)"/> public abstract StoragePath GetPath(KnownFolder knownFolder);
internal static string KnownFolderNotSupported(KnownFolder value) => $"The file system doesn't support or provide a folder matching the " + $"\"{nameof(KnownFolder)}.{value}\" value.";
internal static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder pszPath, KnownFolder nFolder, bool create = false);
public override void OnApplyTemplate() { base.OnApplyTemplate(); string[] txtFiles = new DirectoryInfoEx(KnownFolderIds.DocumentsLibrary).EnumerateFiles("*.txt", SearchOption.TopDirectoryOnly) .Select(fi => fi.FullName) .ToArray(); Console.WriteLine(txtFiles); var tbCSIDL = this.FindName("tbCSIDL") as TextBox; var tbKnownFolder = this.FindName("tbKnownFolder") as TextBox; foreach (var enumItem in Enum.GetValues(typeof(Environment.SpecialFolder))) { Environment.SpecialFolder sf = (Environment.SpecialFolder)enumItem; try { var csidl = IOTools.ShellFolderToCSIDL(sf); if (!csidl.HasValue) { System.Diagnostics.Debugger.Break(); } if (csidl.HasValue) { DirectoryInfoEx di = new DirectoryInfoEx(csidl.Value); di = new DirectoryInfoEx(sf); tbCSIDL.Text += String.Format("SpecialFolder={0}, Label={1}, Csidl={2}, Guid={3}", di.ShellFolderType, di.Label, csidl, di.FullName); } } catch (ArgumentException ex) { tbCSIDL.Text += String.Format("SpecialFolder={0} Not supported", sf); } tbCSIDL.Text += Environment.NewLine; } foreach (var f in KnownFolder.GetKnownFolders()) { string path = f.Path; if (path != null) { DirectoryInfoEx di = new DirectoryInfoEx(path); if (di.KnownFolderType != null) { tbKnownFolder.Text += String.Format("C={0}, N={1}, KFID={2}, Path={3}", di.KnownFolderType.Category, di.KnownFolderType.Definition.Name, di.KnownFolderType.KnownFolderId, di.FullName) + Environment.NewLine; } } //tbKnownFolder.Text += String.Format("{0}, {1}, {2}", f.Category, f.Definition.LocalizedName, f.Path) + Environment.NewLine; } //var knownFolder = KnownFolder.FromCsidl(ShellAPI.CSIDL.CSIDL_COMMON_DOCUMENTS); //var di1 = new DirectoryInfoEx(KnownFolder.FromCsidl(ShellAPI.CSIDL.CSIDL_COMMON_DOCUMENTS)); //knownFolder = KnownFolder.FromDirectoryPath(@"C:\", KnownFolderFindMode.ExactMatch); //knownFolder.pi //var knownFolder = KnownFolders.GetKnownFolder(KnownFolderIdentifiers.LocalAppData); ////KnownFolder.IKnownFolder foundFolder; ////KnownFolders._knownFolderManager.FindFolderFromPath(knownFolder.Path, KnownFolderFindMode.ExactMatch, //// out foundFolder); //tb.Text += // String.Format("{0}", di1); }
/// <summary> /// Gets the current path to the specified known folder as currently configured. This does not require the /// folder to be existent. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <param name="defaultUser">Specifies if the paths of the default user (user profile template) will be used. /// This requires administrative rights.</param> /// <returns>The default path of the known folder.</returns> /// <exception cref="ExternalException">Thrown if the path could not be retrieved.</exception> public static string GetPath(KnownFolder knownFolder, bool defaultUser) { return(GetPath(knownFolder, KnownFolderFlags.DontVerify, defaultUser)); }
/// <summary> /// Gets the current path to the specified known folder as currently configured. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <param name="flags">The known folder flags to use.</param> /// <returns>The default path of the known folder, or an empty string if the path couldn't be retrieved.</returns> public static string GetPath(KnownFolder knownFolder, NativeMethods.KnownFolderFlags flags) { return(GetPath(knownFolder, flags, false)); }
public void ChangeDirectoryToKnownFolder(KnownFolder knownFolder) { string path = KnownFolders.GetPath(knownFolder); ChangeDirectory(path); }
/// <summary> /// Creates and returns a <see cref="StorageFolder"/> instance which represents a specific /// folder which is typically present in a file system. /// </summary> /// <param name="knownFolder"> /// A folder which is typically present in a file system. /// </param> /// <returns> /// A new <see cref="StorageFolder"/> instance which represents the folder represented by /// the <paramref name="knownFolder"/> parameter. /// </returns> /// <exception cref="ArgumentException"> /// <paramref name="knownFolder"/> is an invalid <see cref="KnownFolder"/> enumeration value. /// </exception> /// <exception cref="NotSupportedException"> /// The requested folder is not supported by this file system implementation. /// </exception> /// <seealso cref="TryGetFolder(KnownFolder, out StorageFolder?)"/> public StorageFolder GetFolder(KnownFolder knownFolder) => GetFolder(GetPath(knownFolder));
/// <summary> /// Shows the library management dialog which enables users to mange the library folders and default save location. /// </summary> /// <param name="knownFolder">A known folder based library</param> /// <param name="hwndOwner">The parent windows</param> /// <param name="title">A title for the library management dialog, or NULL to use the library name as the title</param> /// <param name="instruction">An optional help string to display for the library management dialog</param> /// <param name="allowAllLocations">If true, do not show warning dialogs about locations that cannot be indexed</param> /// <returns>true if the user cliked O.K, false if the user clicked Cancel</returns> public static bool ShowManageLibraryUI(KnownFolder knownFolder, IntPtr hwndOwner, string title, string instruction, bool allowAllLocations) { return(ShowManageLibraryUI(knownFolder.Path, hwndOwner, title, instruction, allowAllLocations)); }
/// <summary> /// Creates and initializes the known folder. /// </summary> /// <param name="knownFolder">The known folder which will be initialized.</param> /// <exception cref="ExternalException">Thrown if the known folder could not be initialized.</exception> public static void Initialize(KnownFolder knownFolder) { Initialize(knownFolder, false); }
/// <summary> /// Gets the current path to the specified known folder as currently configured. This does /// not require the folder to be existent. /// </summary> /// <param name="knownFolder">The known folder which current path will be returned.</param> /// <param name="defaultUser">Specifies if the paths of the default user (user profile /// template) will be used. This requires administrative rights.</param> /// <returns>The default path of the known folder.</returns> /// <exception cref="System.Runtime.InteropServices.ExternalException">Thrown if the path /// could not be retrieved.</exception> public static string GetPath(KnownFolder knownFolder, bool defaultUser) { return GetPath(knownFolder, KnownFolderFlags.DontVerify, defaultUser); }
/// <summary> /// Creates and initializes the known folder. /// </summary> /// <param name="knownFolder">The known folder which will be initialized.</param> /// <param name="defaultUser">Specifies if the paths of the default user (user profile /// template) will be used. This requires administrative rights.</param> /// <exception cref="ExternalException">Thrown if the known folder could not be initialized.</exception> public static void Initialize(KnownFolder knownFolder, bool defaultUser) { GetPath(knownFolder, KnownFolderFlags.Create | KnownFolderFlags.Init, defaultUser); }
private static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder pszPath, KnownFolder nFolder, bool create = false);
public List <DirectoryEntry> GetNamespaceDirectories() { var result = new Dictionary <DirectoryEntry, int>(); try { using (var rootKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Classes\CLSID")) { if (rootKey != null) { foreach (var subKeyName in rootKey.GetSubKeyNames()) { using (var possibleEntryRegKey = rootKey.OpenSubKey(subKeyName)) { if ((int?)possibleEntryRegKey?.GetValue("System.IsPinnedToNameSpaceTree", 0) != 1) { continue; } using (var infoKey = possibleEntryRegKey.OpenSubKey("Instance\\InitPropertyBag")) { DirectoryEntry entry; var folderPath = (string)infoKey?.GetValue("TargetFolderPath", null); if (folderPath != null) { using (var directory = new DirectoryInfoEx(folderPath)) entry = GetDirectoryEntry(directory, null); } else if ((folderPath = (string)infoKey?.GetValue("TargetKnownFolder")) != null && Guid.TryParse(folderPath, out var folderId)) { try { using (var directory = new DirectoryInfoEx(KnownFolder.FromKnownFolderId(folderId))) entry = GetDirectoryEntry(directory, null); } catch (Exception) { continue; } } else { continue; } if (entry == null) { continue; } var label = (string)possibleEntryRegKey.GetValue(""); if (!string.IsNullOrEmpty(label)) { entry.Label = label; } result.Add(entry, (int?)possibleEntryRegKey.GetValue("SortOrderIndex", null) ?? int.MaxValue - 1); } } } } } } catch (Exception e) { // Requested registry access is not allowed Log.Logger.Warning(e, "Error when accessing registry at SOFTWARE\\Classes\\CLSID"); } result.Add(GetDirectoryEntry(DirectoryInfoEx.RecycleBinDirectory, null), -1); return(result.OrderBy(x => x.Value).Select(x => x.Key).ToList()); }