简体   繁体   中英

open window explorer folder from my results c#

iv made a web forum, as i have lots of folders on my local drive i can now search for any folders i want on webpage.

Now am looking to add a link to the results of the search so it takes me directly to the folder.

My code in c#:

protected void List_Dirs(string searchStr = null)
    {
        try
        {
            MainContentLocal.InnerHtml = "";
            string[] directoryList = System.IO.Directory.GetDirectories("\\\\myfiles\\Web");
            int x = 0;
            foreach (string directory in directoryList)
            {

                if (searchStr != null && searchStr.Length > 1)
                {
                    UserInfo.Text = "Your Search for :  <strong>" + SearchPhrase.Text + "</strong>  returns  ";
                    if(directoryP.ToLower().Contains(searchStr.ToLower()))
                    {
                        MainContentLocal.InnerHtml += directoryP + "<br />";
                        x++;
                    }
                }
                else
                {
                    MainContentLocal.InnerHtml += directoryP + "<br />";
                }
            if (searchStr != null && searchStr.Length > 1)
            {
                UserInfo.Text += "<strong>" + x.ToString() + "</strong> results";
                UserInfo.CssClass = "userInfo";
            }
        }
        catch(Exception DirectoryListExp)
        {
            MainContentLocal.InnerHtml = DirectoryListExp.Message;
        }
    }

When i enter something is search i will get a list of folders like:

Your Search for : project returns 2 results
job234 project234 Awards
job323 project game

now is there any way for me to click the result so i can open a window explore on the webpage

Thanks

If your result is a file, you can open that file programmatically through the Process class by invoking Process.Start("C:\\\\MyResults.txt") . This will open the results in the default text editor. In the same way, you can also open a web page by inserting passing a Url to Process.Start . I hope this is what wanted.

You can create links like <a href="ThisScript.aspx?folder=project234">project234</a> .

string folder = "\\\\myfiles\\Web";

if (string.IsNullOrWhiteSpace(Request["folder"])) {
  // Folder clicked

  folder = string.Format("{0}{1}", folder, Request["folder"]);
  Process.Start(folder);
}

string[] directoryList = System.IO.Directory.GetDirectories(folder);

Then it will open it on the server. So if it really is local, than it will work. If there is no security problem. But I'm not sure. You can also use file:// links (as Ryan Mrachek notes), but browsers are not happy to let you open them.

our file urls are malformed. It should be:

file:///c:/folder/ Please refer to The Bizarre and Unhappy Story of File URLs .

This works for me:

link When you click Link, a new Windows Explorer window is opened to the specified location. But as you point out, this only works from a file:// URL to begin with.

A detailed explanation of what is going on can be found here. Basically this behavior by design for IE since IE6 SP1/SP2 and the only way you can change it is by explicitly disabling certain security policies using registry settings on the local machine.

So if you're an IT admin and you want to deploy this for your internal corporate LAN, this might be possible (though inadvisable). If you're doing this on some generic, public-facing website, it seems impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM