W moim programie C# muszę przeglądać katalogi. Używam więc metody System.IO.Directory.GetFiles(directory)
i działa dobrze, gdy katalog jest prawdziwym katalogiem, takim jak "C: \ Program File", ale gdy jest to katalog wirtualny (np .: katalog librairie), wartość katalogu wygląda następująco: ":: {031E4825 -7B94-4dc3-B131-E946B44C8DD5} \ Pictures.library-ms "i nie wiem, jak to przeglądać.Jak mogę przeglądać lokalny folder wirtualny w C#?
Odpowiedz
Trzeba przetłumaczyć ścieżkę wirtualną w fizycznym, spróbuj:
DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("your virtual folder here"));
Być może chcesz przeczytać na DirectoryInfo. Jeśli to nie ma sensu, dać to spróbować zamiast:
DirectoryInfo info = new DirectoryInfo("virtual folder here");
FileInfo[] files = info.GetFiles("*.*", SearchOption.AllDirectories);
Wiem, że to szalone stary, ale w przypadku gdy ktoś chce rozwiązania, oto co ja zorientowali się w ciągu ostatniego pół dnia, patrząc na to . Istnieje kilka rozwiązań, które mogą Ci pomóc uzyskać the folder names if you give it the path to the Virtual Folder's XML location, ale nic, co widziałem, nie dostanie Cię tam od ::{031E4825-....}
. Był hint in another question's answer, aby użyć przykładu KnownFolders Browser WindowsAPicodePack. Tak czytam poprzez kod źródłowy, w tym i mają pochodzić z następujących czynności:
Oto DialogBox używałem dostać foldery, a ja pozwoliło na AllowNonFileSystemItems, co pozwala Biblioteka pozycje folderu:
Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog dlg = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
dlg.Title = "Pick Folder";
dlg.IsFolderPicker = true;
dlg.InitialDirectory = Environment.SpecialFolder.Personal.ToString(); // If default setting does not exist, pick the Personal folder
dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = true;
dlg.DefaultDirectory = dlg.InitialDirectory;
dlg.EnsurePathExists = true;
dlg.EnsureFileExists = false;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = true;
dlg.ShowPlacesList = true;
if (dlg.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
{
foreach (string dirname in dlg.FileNames)
{
var libFolders = ExpandFolderPath(dirname);
if (libFolders == null)
{
MessageBox.Show("Could not add '" + dirname + "', please try another.");
}
else
{
foreach (string libfolder in libFolders)
{
DoWork(libfolder);
}
}
}
}
I następnie iterować nad allSpecialFolders
, aby znaleźć ten sam ::{031E4825-...}
, który jest ParsingName dla SpecialFolder (tak, prawdopodobnie bardziej elegancki sposób). Po tym, należy użyć XML odczyt z innymi rozwiązaniami (I used a CodeProject example that did the same thing), aby uzyskać folderów w tym folderze biblioteki:
/// <summary>Gets the folders associated with a path</summary>
/// <param name="libname"></param>
/// <returns>Folder, or List of folders in library, and null if there was an issue</string></returns>
public List<string> ExpandFolderPath(string foldername)
{
List<string> dirList = new List<string> { };
// If the foldername is an existing directory, just return that
if (System.IO.Directory.Exists(foldername))
{
dirList.Add(foldername);
return dirList;
}
// It's not a directory, so check if it's a GUID Library folder
ICollection<IKnownFolder> allSpecialFolders = Microsoft.WindowsAPICodePack.Shell.KnownFolders.All;
Regex libguid = new Regex(@"\b([A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12})\b");
var match = libguid.Match(foldername);
if (match == null)
return null;
string fpath = "";
// Iterate over each folder and find the one we want
foreach (var folder in allSpecialFolders)
{
if (folder.ParsingName == foldername)
{
// We now have access to the xml path
fpath = folder.Path;
break;
}
}
if (fpath == "")
{
// Could not find it exactly, so find one with the same prefix, and
// replace the filename
foreach (var folder in allSpecialFolders)
{
if (folder.ParsingName.Contains(match.Groups[1].Value))
{
string sameDir = System.IO.Path.GetDirectoryName(folder.Path);
string newPath = System.IO.Path.Combine(sameDir, match.Groups[2].Value);
if (System.IO.File.Exists(newPath))
fpath = newPath;
break;
}
}
}
if (fpath == "")
return null;
var intFolders = GetLibraryInternalFolders(fpath);
return intFolders.Folders.ToList();
}
/// <summary>
/// Represents an instance of a Windows 7 Library
/// </summary>
public class Win7Library
{
public Win7Library()
{
}
public string Name { get; set; }
public string[] Folders { get; set; }
}
[DllImport("shell32.dll")]
static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);
//Handles call to SHGetKnownFolderPath
public static string getpathKnown(Guid rfid)
{
IntPtr pPath;
if (SHGetKnownFolderPath(rfid, 0, IntPtr.Zero, out pPath) == 0)
{
string s = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
return s;
}
else return string.Empty;
}
private static string ResolveStandardKnownFolders(string knowID)
{
if (knowID.StartsWith("knownfolder:"))
{
return getpathKnown(new Guid(knowID.Substring(12)));
}
else
{
return knowID;
}
}
private static Win7Library GetLibraryInternalFolders(string libraryXmlPath)
{
Win7Library newLibrary = new Win7Library();
//The Name of a Library is just its file name without the extension
newLibrary.Name = System.IO.Path.GetFileNameWithoutExtension(libraryXmlPath);
List<string> folderpaths = new List<string>();
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); //* create an xml document object.
xmlDoc.Load(libraryXmlPath); //* load the library as an xml doc.
//Grab all the URL tags in the document,
//these point toward the folders contained in the library.
System.Xml.XmlNodeList directories = xmlDoc.GetElementsByTagName("url");
foreach (System.Xml.XmlNode x in directories)
{
//Special folders use windows7 Know folders GUIDs instead
//of full file paths, so we have to resolve them
folderpaths.Add(ResolveStandardKnownFolders(x.InnerText));
}
newLibrary.Folders = folderpaths.ToArray();
return newLibrary;
}
Nadzieja to pomaga komuś w przyszłości!
Alternatywnie, ['Shell.Application.NameSpace (" :: {031E4825 -....} ") .Items'] (http://blogs.msdn.com/b/oldnewthing /archive/2013/02/04/10390725.aspx). Jeśli szukasz bibliotek, możesz skorzystać z [Biblioteki API] (https://msdn.microsoft.com/en-us/library/windows/desktop/dd758094 (v = vs.85) .aspx). – Mitch
- 1. Jak przeglądać folder
- 2. Jak mogę uzyskać folder ze zdalnego komputera na lokalny komputer?
- 3. Jak mogę przeglądać mój lokalny serwer Tomcat z innego komputera w sieci?
- 4. Wordpress: Jak mogę przeglądać listę postów?
- 5. Jak mogę zainstalować lokalny klejnot?
- 6. Jak zaimportować lokalny projekt/folder GIT/Maven do zaćmienia?
- 7. C#: Utwórz wirtualny dysk w komputerze
- 8. Jak mogę wyświetlić folder .git w Netbeans
- 9. Jak mogę spakować folder w pomruk?
- 10. Jak przeglądać mapę w Thymeleaf
- 11. Jak zdobyć folder% AppData% w C?
- 12. Jak utworzyć wirtualny gamepad?
- 13. Jak mogę przeglądać obraz z biblioteki obrazów iPhone'a?
- 14. Jak przeglądać historię różnych poleceń w Emacs
- 15. Jak mogę przeglądać klasy i metody biblioteki DLL ActiveX?
- 16. C# Folder Sync biblioteka
- 17. Jak mogę dołączyć folder z cx_freeze?
- 18. C++ wirtualny układ tabeli MI (wielokrotne dziedziczenie)
- 19. utworzyć folder programowo w Xcode - Cel C
- 20. Jak równolegle przeglądać dwie listy?
- 21. Jak mogę sprawić, aby host wirtualny akceptował wiele domen?
- 22. Czy mogę przeglądać wszystkie wiadomości e-mail programu Outlook w folderze, w tym w podfolderach?
- 23. Jak znaleźć bieżący terminal wirtualny
- 24. Jak mogę analizować lokalny plik JSON w React js?
- 25. Jak mogę uzyskać lokalny adres IP w Node.js?
- 26. Jak utworzyć folder appdata z C#
- 27. Jak przeglądać Array JSON w PHP
- 28. Wskaźnik wirtualny
- 29. Jak mogę "otworzyć folder w eksploratorze" w Netbeans PHP
- 30. Wirtualny joystick w Javie
Co to jest "Server.MapPath"? Referencje MSDN, które mogłem znaleźć dla MapPath, znajdują się w System.Web ... OP nie określił, że jest to aplikacja internetowa, zresztą z opisu nie przypomina sieci. – poncha
'OP nie określił, że była to aplikacja internetowa'. Nie sprecyzował, że to nie jest ... – dtsg
Masz rację, nie zrobił tego, ale konkretny przykład dał wirtualną ścieżkę do biblioteki systemowej, która jest z pewnością powiązana z siecią ... – poncha