Windows Security Message when opening file

955 views Asked by At

I have a Windows forms desktop application with a webBrowser control. I navigate to a directory on the server webBrowser1.Navigate(new Uri("\\\\srvername\\share\\directory\\")) to allow users to open files from that directory.

enter image description here

When they double click on files they get a Windows Security message every time.

enter image description here

When they click OK, the file opens as desired.

If I set navigate to a local directory webBrowser1.Navigate(new Uri("C:\\Temp\\")); I don't get the message.

Is there a programmatic way to prevent this from showing or is this something that will have to be added to the browser as a trusted site? Standard users on our network don't have access to add trusted sites. I have tried to check the "Include all network paths UNC" in the local intranet section of IE.

I have tried

webBrowser1.ScriptErrorsSuppressed = true;

but that doesn't work as it seems to be meant for script errors happen from a webpage displayed in the control.

1

There are 1 answers

0
MatthewD On BEST ANSWER

Well I found two resolutions to my question.

Is it possible? As @jacob and all my research suggests, I don't think it is. If you are going to use the webBrowser control to view local or UNC path you will have to make changes to IE security or deal with the message for every file that is opened.

Resulution #1
Change security settings in IE. My organization has everything locked down so changes to IE settings can only be made through group policy.

  1. Add file://servername/* to the Intranet Zone via group policy for the desired domain OU.
  2. Enable the IE setting “Launching applications and files in an IFRAME” policy via the group policy for the desired domain OU.

Resulution #2
Replace the webBrowser control in my app with other controls that can accomplish the same functionality. This is one I went with so I would have more control over all working of the files and directories. I also don't have to worry about the security settings and group policy enforcement being correct and working correctly. I also don't have to worry about changes in these areas effecting me.

Here is the article I used to get started

The walk through as it is works pretty well. I went through it in a one off project before integrating it into my project. I need to change a few things to suit me. Here is my code as it ended up. I made some changes and added some selecting/clicking events.

    Boolean FocusTreeview = false;

    public frmMain()
    {
        InitializeComponent();
        treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
    }

    // Start by calling this.
    private void PopulateTreeView()
    {
        TreeNode rootNode;

        treeView1.Nodes.Clear();
        listView1.Items.Clear();

        DirectoryInfo info = new DirectoryInfo("Pass the root folder here.");

        // Add the root directory.
        if (info.Exists)
        {
            rootNode = new TreeNode(info.Name);
            rootNode.Tag = info;
            GetDirectories(info.GetDirectories(), rootNode);
            treeView1.Nodes.Add(rootNode);
            treeView1.Nodes[0].Expand();
            // This selects the root directory
            NodeSelect(rootNode);
        }
    }

    /********************************************/

    private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
    {
        // This will list all the directories and subdirectories
        TreeNode aNode;
        DirectoryInfo[] subSubDirs;
        foreach (DirectoryInfo subDir in subDirs)
        {
            aNode = new TreeNode(subDir.Name, 0, 0);
            aNode.Name = subDir.ToString();
            aNode.Tag = subDir;
            aNode.ImageKey = "Directory";
            subSubDirs = subDir.GetDirectories();
            if (subSubDirs.Length != 0)
            {
                GetDirectories(subSubDirs, aNode);
            }
            nodeToAddTo.Nodes.Add(aNode);
        }
    }

    /********************************************/

    private void NodeSelect(TreeNode newSelected)
    {
        //Load all the files and directorys in the selected node from the treeview
        listView1.Items.Clear();
        DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
        ListViewItem.ListViewSubItem[] subItems;
        ListViewItem item = null;

        foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
        {
            item = new ListViewItem(dir.Name, 0);
            subItems = new ListViewItem.ListViewSubItem[]
              {new ListViewItem.ListViewSubItem(item, "Directory"), 
               new ListViewItem.ListViewSubItem(item, 
            dir.LastAccessTime.ToShortDateString())};
            item.SubItems.AddRange(subItems);
            listView1.Items.Add(item);
        }
        foreach (FileInfo file in nodeDirInfo.GetFiles())
        {
            item = new ListViewItem(file.Name, 1);
            subItems = new ListViewItem.ListViewSubItem[] { new ListViewItem.ListViewSubItem(item, "File"), new ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString()) };

            item.SubItems.AddRange(subItems);
            listView1.Items.Add(item);
        }

        listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
    }

    /********************************************/

    void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        TreeNode newSelected = e.Node;
        NodeSelect(newSelected);
    }

    /********************************************/

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // When something is selected load it's contents to the listview if it is a directory
        if (listView1.SelectedItems.Count > 0)
        {
            var item = listView1.SelectedItems[0];

            // Don't do anything if it's just a file
            if (item.SubItems[1].Text == "File")
            {
                return;
            }

            TreeNode[] tns = treeView1.Nodes.Find(item.Text, true);
            if (tns.Length > 0)
            {
                treeView1.Focus();
                NodeSelect(tns[0]);
                treeView1.SelectedNode = tns[0];
                tns[0].Expand();
                FocusTreeview = true;
            }
        }
    }

    /********************************************/

    private void listView1_MouseUp(object sender, MouseEventArgs e)
    {
        // This needs to be done so the directory appears to stay selected in the treeview
        if (FocusTreeview == true)
        {
            treeView1.Focus();
            FocusTreeview = false;
        }
    }

    /********************************************/

    private void listView1_DoubleClick(object sender, EventArgs e)
    {
        // This will open the files that are double clicked on
        if (listView1.SelectedItems.Count > 0)
        {
            var item = listView1.SelectedItems[0];

            // Don't do anything for directories
            if (item.SubItems[1].Text == "Folder")
            {
                return;
            }

            // Open the selected file
            Process.Start(globals.szJobFolderPath + item.Text);

        }
    }

It ended up looking like this.

enter image description here

You do lose the icons of known files like Word docs but that is not a big deal to me. You could add icons for the known file types you will have to your imagelist and put some if statements (or switch cases) in the section where it adds items to the listview. Just analyze the file extensions.

Hope this helps somebody someday!