Frage

In .Net können wir die Wege zu ‚speziellen Ordner‘, wie Dokumente / Desktop usw. Heute habe ich retreive versucht, einen Weg zu finden, den Weg zum ‚Downloads‘ -Ordner zu bekommen, aber es ist nicht speziell genug scheint es.

Ich weiß, ich kann einfach tun ‚C: \ Users \ Benutzername \ Downloads‘, aber das scheint eine hässliche Lösung. Also, wie kann ich den Weg mit .Net?

retreive
War es hilfreich?

Lösung

Das Problem Ihrer ersten Antwort ist es würden Sie falsches Ergebnis geben, wenn die Standard-Downloads Dir hat [Download1] geändert! Der richtige Weg, es über alle Möglichkeiten zu tun ist,

using System;
using System.Runtime.InteropServices;

static class cGetEnvVars_WinExp    {
    [DllImport("Shell32.dll")] private static extern int SHGetKnownFolderPath(
        [MarshalAs(UnmanagedType.LPStruct)]Guid rfid, uint dwFlags, IntPtr hToken,
        out IntPtr ppszPath);

    [Flags] public enum KnownFolderFlags : uint { SimpleIDList = 0x00000100
        , NotParentRelative = 0x00000200, DefaultPath = 0x00000400, Init = 0x00000800
        , NoAlias = 0x00001000, DontUnexpand = 0x00002000, DontVerify = 0x00004000
        , Create = 0x00008000,NoAppcontainerRedirection = 0x00010000, AliasOnly = 0x80000000
    }
    public static string GetPath(string RegStrName, KnownFolderFlags flags, bool defaultUser) {
        IntPtr outPath;
        int result = 
            SHGetKnownFolderPath (
                new Guid(RegStrName), (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);
        }
    }

}   

Um es zu testen, wenn Sie gezielt Ihre persönlichen Download-dir wünschen, Sie Flag standardmäßig auf false ->

using System.IO;
class Program    {
    [STAThread]
    static void Main(string[] args)        {
        string path2Downloads = string.Empty;
        path2Downloads = 
            cGetEnvVars_WinExp.GetPath("{374DE290-123F-4565-9164-39C4925E467B}", cGetEnvVars_WinExp.KnownFolderFlags.DontVerify, false);
        string[] files = { "" };
        if (Directory.Exists(path2Downloads)) {
            files = Directory.GetFiles(path2Downloads);
        }
    }//Main
}

oder nur eine Zeile Environment.ExpandEnvironmentVariables () -> (die einfachste Lösung).

using System.IO;
class Program    {
/* https://ss64.com/nt/syntax-variables.html */
    [STAThread]
    static void Main(string[] args)        {
        string path2Downloads = string.Empty;
        string[] files = { "" };
        path2Downloads = Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Downloads");
        if (Directory.Exists(path2Downloads)) {
            files = Directory.GetFiles(path2Downloads);
        }
    }//Main
}

Andere Tipps

Ja, es speziell ist, hat den Namen dieses Ordners zu entdecken nicht möglich, bis Vista. .NET muss noch vor Betriebssysteme unterstützen. Sie können SHGetKnownFolderPath () zu umgehen diese Einschränkung, wie diese pinvoke:

using System.Runtime.InteropServices;
...

public static string GetDownloadsPath() {
    if (Environment.OSVersion.Version.Major < 6) throw new NotSupportedException();
    IntPtr pathPtr = IntPtr.Zero;
    try {
        SHGetKnownFolderPath(ref FolderDownloads, 0, IntPtr.Zero, out pathPtr);
        return Marshal.PtrToStringUni(pathPtr);
    }
    finally {
        Marshal.FreeCoTaskMem(pathPtr);
    }
}

private static Guid FolderDownloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
private static extern int SHGetKnownFolderPath(ref Guid id, int flags, IntPtr token, out IntPtr path);

Versuchen Sie diese:

string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)+ @"\Downloads";

Ich habe die folgenden Code und es funktioniert für .net 4.6 mit Windows 7 und höher. Der Code unten gibt das Benutzerprofil Ordnerpfad -> "C:\Users\<username>"

string userProfileFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

Als nächstes werden die Downloads zuzugreifen nur zusätzlichen Pfad Strings kombinieren Ordner wie folgt:

string DownloadsFolder = userProfileFolder + "\\Downloads\\";

Nun wird das Endergebnis sein

"C:\Users\<username>\Downloads\"

Hope es spart Zeit, dass jemand in der Zukunft.

versuchen:

Dim Dd As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
Dim downloD As String = Dd.Replace("Favorites", "Downloads")
txt1.text = downLoD

Es ist nur ein Trick, nicht Lösung.

Für VB, versuchen ...

Dim strNewPath As String = IO.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) + "\Downloads\"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top