在我的C#程序中,我必須瀏覽目錄。 因此,我使用方法 System.IO.Directory.GetFiles(directory)
,當目錄是像「C:\ Program File」這樣的真實目錄,但是當它是一個虛擬目錄(例如:librairie目錄)時,目錄值如下所示:「:: {031E4825 -7B94-4dc3-B131-E946B44C8DD5} \ Pictures.library-ms「,我不知道如何瀏覽它。如何在C#中瀏覽本地虛擬文件夾?
回答
您需要翻譯的虛擬路徑轉換成物理,請嘗試:
DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("your virtual folder here"));
你可能想在DirectoryInfo閱讀起來。如果這是沒有用的,給這個試試這個:
DirectoryInfo info = new DirectoryInfo("virtual folder here");
FileInfo[] files = info.GetFiles("*.*", SearchOption.AllDirectories);
我知道這是老瘋子,但如果有人想解決辦法,這裏是我在過去半天尋找這個想通了。有幾個解決方案可以讓你the folder names if you give it the path to the Virtual Folder's XML location,但沒有看到讓你從::{031E4825-....}
那裏。有一個hint in another question's answer使用WindowsAPICodePack的KnownFoldersBrowser示例。因此,我通過在源代碼中讀取並想出了以下內容:
這裏是對話框我用得到的文件夾,和我已經啓用它AllowNonFileSystemItems,這使得庫文件夾選項:
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);
}
}
}
}
然後,我遍歷allSpecialFolders
找到相同的::{031E4825-...}
這是SpecialFolder的ParsingName(是的,可能是更優雅的方式)。在此之後,使用XML從其他解決方案(I used a CodeProject example that did the same thing)閱讀得到的文件夾在庫文件夾:
/// <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;
}
希望這可以幫助別人的未來!
或者,['Shell.Application.NameSpace(「:: {031E4825 -....}」).Items'](http://blogs.msdn.com/b/oldnewthing /archive/2013/02/04/10390725.aspx)。如果您只查找庫,則可以使用[庫API](https://msdn.microsoft.com/en-us/library/windows/desktop/dd758094(v = vs.85).aspx)。 – Mitch
- 1. 在C#中瀏覽文件夾VS2005
- 2. 如何在java中瀏覽文件夾?
- 3. 如何瀏覽文件夾
- 4. 虛擬文件夾
- 5. 如何使用C++在Windows中添加虛擬文件夾
- 6. 從網絡瀏覽器打開本地文件夾瀏覽器
- 7. 從瀏覽器中打開本地文件/文件夾
- 8. C#中的文件夾瀏覽器#
- 9. 我該如何在本地託管web瀏覽器虛擬機:http://bellard.org/jslinux/
- 10. 通過本地VBA瀏覽https SharePoint文件和文件夾
- 11. win32 api虛擬文件夾
- 12. Java虛擬文件夾?
- 13. UNC虛擬文件夾
- 14. 如何在C#中的FTP文件夾中的本地機器上創建虛擬驅動器?
- 15. 將Azure虛擬機本地文件夾備份到blob存儲?
- 16. 如何瀏覽Objective C中的文件夾?
- 17. C++ winapi SHBrowseForFolder瀏覽隱藏文件夾
- 18. 如何在Microsoft Edge瀏覽器中打開本地HTML文件?
- 19. 如何在瀏覽器中本地執行.js文件?
- 20. 如何在Android瀏覽器中加載本地HTML文件
- 21. 如何瀏覽文件夾用空格
- 22. 如何瀏覽特定文件夾
- 23. 如何瀏覽文件夾/ MAMP?
- 24. 如何在Rails應用程序中配置虛擬文件夾?
- 25. pyvmomi python:如何在特定文件夾中創建虛擬機
- 26. 從虛擬文件夾檢索文件
- 27. 使用shell腳本瀏覽文件夾
- 28. Linux中的「虛擬文件夾」
- 29. 虛擬文件夾中的USB設備
- 30. 在ASP.net中的文件夾瀏覽
什麼是「Server.MapPath」?我可以找到MapPath的MSDN引用都在System.Web內... OP沒有指定這是一個Web應用程序,而且從描述中看,它看起來不像web。 – poncha
'OP沒有指定這是一個Web應用程序。他沒有說明它不是... – dtsg
你是對的,他沒有,但他給出的具體例子是一個系統庫的虛擬路徑,這與系統可疑有關... – poncha