Search code examples
c#wpfdirectoryconnectionremote-access

C# : Open a folder of a remote PC into the Windows files explorer


I want to open a folder of a remote PC into the Windows files explorer : 1

I wrote the following method :

private void openComponentFolder_Click(object sender, RoutedEventArgs e)
        {
            #region Connexion to the bench
            ImpersonatedUser iuImpersonatedUser = new ImpersonatedUser(BenchInfos.Current.UserName, "user", BenchInfos.Current.PcPassword);

            iuImpersonatedUser.LogOn();
            #endregion

            Component component = (sender as MenuItem).DataContext as Component;

            if (component != null)
                if (Directory.Exists(component.Path))
                    Process.Start(component.Path);

            #region Disconnexion
            iuImpersonatedUser.LogOut();
            iuImpersonatedUser.Dispose();
            #endregion
        }

When I click on a Component on a wpf treeview, I connect to the remote PC (with the UserName BenchInfos.Current.UserName, the domain "user", and the password BenchInfos.Current.PcPassword of the PC), and if the Component path (component.Path) exists on the remote PC, the folder (component.Path) should be open in the Windows files explorer. Finally I disconnect to the remote PC.

However, when the method is run, the access to the folder is denied ("Accès refusé") while the path exists:
enter image description here

Moreover, in other method, I'm able to read files in component.Path folders...

So why, am I not able to open the folder into the Windows file explorer and what can I do to make it working ?

Thank you for your help.

Rémi


Solution

  • I found a solution :

    private void openComponentFolder_Click(object sender, RoutedEventArgs e)
    {
        Component component = (sender as MenuItem).DataContext as Component;
    
        ProcessStartInfo startInfo = new ProcessStartInfo()
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "net",
            Arguments = "use " + Path.Combine(ConfigMorphee.Instance.PcPath, "c$") + " /user:" + BenchInfos.Current.UserName + " " + BenchInfos.Current.PcPassword
        };
    
        Process process = new Process() { StartInfo = startInfo };
    
        process.Start();
        process.WaitForExit();
    
        if (component != null)
        {
            if (Directory.Exists(component.Path))
            {
                try
                {
                    Process.Start(component.Path);
                }
                catch (Exception ex)
                {
                    Logger.Error("Catch MainWindow - openComponentFolder_Click ", ex);
                }
    
            }
        }
    }