簡體   English   中英

C#FolderBrowserDialog-如何包括共享文件夾

[英]C# FolderBrowserDialog - how to include shared folders

我按如下方式創建FolderBrowserDialog(僅摘錄-不完整的代碼):

   string tempSetWorkingPath = null;
    try
    {
        FolderBrowserDialog folderDlg = new System.Windows.Forms.FolderBrowserDialog();

        folderDlg.ShowNewFolderButton = true;
        folderDlg.Description = "Selected your working folder. This is where your PDF files will be saved.";
        folderDlg.RootFolder = Environment.SpecialFolder.MyComputer;
        folderDlg.SelectedPath = (Convert.ToString(WorkingPath).Trim().Length == 0) ? ((int)Environment.SpecialFolder.MyComputer).ToString() : WorkingPath;

        if (folderDlg.ShowDialog() == DialogResult.OK)
        {
            tempSetWorkingPath = folderDlg.SelectedPath;
        }
        else
        {
            tempSetWorkingPath = "";
        }
    }

...

該代碼運行良好,只是顯示的唯一文件夾是本地文件夾。 用戶在其系統上具有DropBox和OneDrive共享文件夾,並且要選擇這些目錄之一,用戶需要循環瀏覽Windows用戶目錄並從中選擇文件夾。 在過去幾個月中看到的某些系統上,我已經看到DropBox和OneDrive目錄出現在DeskTop目錄下...但是,盡管經過了數小時的搜索,我仍然沒有找到找到實現該目標的方法。

我該如何實現?

MTIA

DWE

鑒於我已經觀察到在這里和其他地方發布的有關目錄包括共享目錄在內的大量查詢,並且給出了@ Mailosz的響應,因此文件夾對話框的根文件夾屬性似乎擁有鍵- 它獲取或設置瀏覽開始的根文件夾,這就是我的代碼丟失的原因。

我的問題中提及的功能的完整代碼顯示在下面,以幫助其他人。

/// <summary>
/// presents the user with a folder dialog
/// Returns a full qualified directory chosen by the user
/// </summary>
/// <param name="WorkingPath">if a fully qualified directory name is provided, then the folder structure in the folder dialog will open to the directory selected</param>
/// <returns>Returns a full qualified directory chosen by the user or if no directory was chosen, an empty string</returns>
public string SetWorkingPath(string WorkingPath)
{
    string tempSetWorkingPath = null;
    try
    {
        FolderBrowserDialog folderDlg = new System.Windows.Forms.FolderBrowserDialog();
        // check our proposed working path and if its valid
        if((!string.IsNullOrEmpty(WorkingPath) && (WorkingPath != null)))
        {
            if (!Directory.Exists(WorkingPath))
                  WorkingPath = string.Empty;
        }
        else // if we are empty or null set us to empty
        {
            WorkingPath = string.Empty;
        }
        folderDlg.ShowNewFolderButton = true;
        folderDlg.Description = "Please select your working folder. This is where your PDF files will be saved.";
        folderDlg.RootFolder = Environment.SpecialFolder.Desktop;//.MyComputer;
        folderDlg.SelectedPath = (Convert.ToString(WorkingPath).Trim().Length == 0) ? ((int)Environment.SpecialFolder.MyComputer).ToString() : WorkingPath;

        if (folderDlg.ShowDialog() == DialogResult.OK)
        {
            // make sure we have a backslash on the end of our directory string
            tempSetWorkingPath = PathAddBackslash(folderDlg.SelectedPath);
        }
        else
        {
            // return an empty string
            tempSetWorkingPath = string.Empty;
        }
    }
    catch (Exception ex)
    {
        tempSetWorkingPath = string.Empty;

        throw (ex);
    }

    return tempSetWorkingPath;
}
public string PathAddBackslash(string path)
{
    // They're always one character but EndsWith is shorter than
    // array style access to last path character. Change this
    // if performance are a (measured) issue.
    string separator1 = Path.DirectorySeparatorChar.ToString();
    string separator2 = Path.AltDirectorySeparatorChar.ToString();

    // Trailing white spaces are always ignored but folders may have
    // leading spaces. It's unusual but it may happen. If it's an issue
    // then just replace TrimEnd() with Trim(). Tnx Paul Groke to point this out.
    path = path.TrimEnd();

    // Argument is always a directory name then if there is one
    // of allowed separators then I have nothing to do.
    if (path.EndsWith(separator1) || path.EndsWith(separator2))
        return path;

    // If there is the "alt" separator then I add a trailing one.
    // Note that URI format (file://drive:\path\filename.ext) is
    // not supported in most .NET I/O functions then we don't support it
    // here too. If you have to then simply revert this check:
    // if (path.Contains(separator1))
    //     return path + separator1;
    //
    // return path + separator2;
    if (path.Contains(separator2))
        return path + separator2;

    // If there is not an "alt" separator I add a "normal" one.
    // It means path may be with normal one or it has not any separator
    // (for example if it's just a directory name). In this case I
    // default to normal as users expect.
    return path + separator1;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM