0

I have a windows application which calls a shortcut to a folder on a server which requires domain users login (username and password). If I run the shortcut directly then I get the windows security login window which by default set the domain as the local computer, if I entered the credentials as mydomain\myuser then the password then it opens normal. But when it's called from my application using the below code I get the error "This operation was canceled by user" and no windows security login window doesn't appear. But if I run the shortcut from the windows explorer and logged in using the windows security login window then I run my application it will directly open the server folder. So I wonder how I can make the windows security login window appear when I run my application as if I clicked on the shortcut normally.

        string desktoppath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        Process proc2 = new Process();
        proc2.StartInfo.FileName = desktoppath + @"\ServerFolder.lnk";
        proc2.Start();
        proc2.Close();

This is the error message I get:

enter image description here

And this is the details:

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): The operation was canceled by the user
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at MyApp_ServerConnection.Form1.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4110.0 built by: NET48REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
MyApp-ServerConnection
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/MyApp-Connect-Disconnect/MyApp-ServerConnection.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4110.0 built by: NET48REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4110.0 built by: NET48REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3761.0 built by: NET48REL1
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3761.0 built by: NET48REL1
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.4110.0 built by: NET48REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.8.3761.0 built by: NET48REL1
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Tak
  • 3,536
  • 11
  • 51
  • 93
  • You would have mentioned UAC. Could be anti-malware stepping in, a .exe file that appears from seemingly no-where, next prompting for a network password is of course a strong liability. Temporarily disable it to see if that changes the outcome. – Hans Passant Apr 17 '20 at 22:57
  • @HansPassant I disabled UAC following the steps in this link but still the same problem https://www.firehousesoftware.com/webhelp/FH/Content/UACInstallationInstructions/02_Win7DisableUAC.htm – Tak Apr 18 '20 at 00:04
  • No, temporarily disable the installed anti-malware product. – Hans Passant Apr 18 '20 at 02:06
  • @HansPassant I tried this but still the same error message. Any advice? – Tak Apr 22 '20 at 18:39
  • @HansPassant May be there is a way to connect to the share path from the application itself if I provided a username and password textboxs then use these credentials somehow to access the link? – Tak Apr 22 '20 at 18:46

1 Answers1

-1

When you started the Process, you are immediately calling the Close() method, thus canceling the operation by the user. You could call proc2.WaitForExit() (Link) and then call proc2.Close() in the method that is called when the Exited event fires.

See Process.Exited for more information on how to create the event.

Brett Wertz
  • 412
  • 4
  • 19
  • I tried this but still the same error. I've updated my question with a screenshot of the error and its details as well. – Tak Apr 17 '20 at 22:23
  • Try it like this: https://stackoverflow.com/a/23358242/1886644 – Brett Wertz Apr 17 '20 at 22:29
  • What's the difference? I mean it works if the user was already logged in so I am not sure, but I can try. – Tak Apr 17 '20 at 22:35