简体   繁体   中英

ACCESS_DENIED in UWP app using ShellExecute

I am converting a C# WinForm app to UWP.

The old app is using pinvoke ShellExecute API in order to open some files. I am trying to copy the same code to the new app, but get an exception.

Here is my code:

     public static readonly int SHELL_EXECUTE_ERROR_CODE_LIMIT = 32;
     [DllImport("shell32.dll")]
        static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            /*ShowCommands*/int nShowCmd);

    private void btnPinvoke_Click(object sender, RoutedEventArgs e)
    {
        int ret = 0;
        IntPtr retPtr = ShellExecute(IntPtr.Zero, "open", @"C:\path\to\file\dummy.pdf", "", "", /*ShowCommands.SW_SHOWNOACTIVATE*/4);
        ret = retPtr.ToInt32();
        if (ret <= SHELL_EXECUTE_ERROR_CODE_LIMIT) //Failure
        {
            lblResult.Text = "Error " + ret;
        }
    }

The error code which is returning is 5.

ERROR_ACCESS_DENIED

5 (0x5)

Can you help me understand why am I getting ACCESS_DENIED exception?
It happens also when running Visual Studio as Administrator.

Maybe you can try

Diagnostics.Process.Start(@"C:\path\to\file\dummy.pdf");

I used this many times in my applications and it works

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