System.Diagnostics.Process.Start Exception for different user - c#

I created an exe application with a button which has the following code executed when clicked. It is working when clicked by the same user which is used to create the exe. But the same exe when clicked by other users get exception message as "Application not found". Also the stack trace looks like given below.
try
{
System.Diagnostics.Process.Start("https://google.com");
} catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Exception:
Exception Info: System.ComponentModel.Win32Exception
at System.Diagnostics.Process.StartWithShellExecuteEx(System.Diagnostics.ProcessStartInfo)
at System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo)
at MyTool.MainWindow.MyButton_Click(System.Object, System.Windows.RoutedEventArgs)

Maybe the other users that are running your .exe don't have a default browser or application to run this command. You can validate my theory if you execute the command in Run (Windows+R) and you encounter an error.

You're trying to start application named "https://google.com". You need to start web-browser application with parameter as URL:
System.Diagnostics.Process.Start("chrome.exe", "https://google.com");
Or with other application name.

Related

Process.Start throws exception System.ComponentModel.Win32Exception: "The operation was canceled by the user" the first time it is started after login

We have created a deployment solution that checks for updates and starts a shortcut. Now i have a very strange behaviour. When i login to our RDS-Host (Windows Server 2019 Standard - 1809) the first time i start the tool it throws an exception at this line of code:
Process.Start(#"C:\Users\dev.dmendez\AppData\Local\simplDeploy\packages\https!!!om-apps.com!csa!om_cs\furniture\12.10.2.0008\OM_CS.lnk");
It throws this exception and the process is not started:
System.ComponentModel.Win32Exception: "The operation was canceled by the user"
If i run it a second time the exception is not thrown and the process is started normaly. There is no UAC-Dialog poping up (UAC is activated). I've tried to change the target-Shortcut to require administrator-permission. With this setting active i get an UAC-Dialog and if cancel i get the exact same exception-message. It looks like windows somehow tries to start the shortcut as admin and automatically cancels the first time i run it after login.
I also tried Logon/Logout then run the target-shortcut manually before i run the program that also calls the shortcut and... No Exception!
Any ideas why i get this exception the first time i execute this line of code after login.
I tried many things and ended up with this:
try
{
Process.Start(Manifest.GetExecutablePath());
}
catch (Win32Exception) // in some cases there happens an exception: "The operation was canceled by the user" on the first launch
{
Process.Start(Manifest.GetExecutablePath());
}
I know it's nasty, but the exception reliable appears the first time after login and the second time it works perfectly, therefore i just ignore it one time and try again.

Fusion Log Assembly Binder Error - Bind result: hr = 0x1. Incorrect function

I am trying to get to the bottom of a strange behavior on one machine. I have a trivial console application that will run interactively, but when I invoke it via WMI, it will start and exit immediately.
I enabled the Fusion log, since Procmon was unrevealing. I see the following error:
*** Assembly Binder Log Entry (31-01-2015 # 19:22:51) ***
The operation was successful.
Bind result: hr = 0x1. Incorrect function.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\CMCBOOTSTRAP\Cmc.Installer.Agent.Console.exe
--- A detailed error log follows.
BEGIN : Native image bind.
END : Incorrect function. (Exception from HRESULT: 0x00000001 (S_FALSE))
What is the cause for "incorrect function"? What else can I look at to determine why this application effectively dies on startup via WMI?
And I mean trivial...
class Program
{
static void Main(string[] args)
{
Thread.Sleep(30000);
}
}
Environment is Windows Server 2012 R2 and .NET 4.5.
This is an entirely normal mishap, you got it from Fuslogvw.exe by selecting the "Native Images" radio button in the Log Categories setting. Readily reproducible on my own machine as well, I see many of them.
The actual error code is S_FALSE, a COM error code that means "it successfully failed". Which is why it says The operation was successful. Misinterpreted for the diagnostic message as "Function failed", that's the description for Windows error 1 and returned by the FormatMessage() winapi function.
The successful failure is entirely expected, you didn't run Ngen.exe on your console mode app yet so the native image for it is not available. Keep looking, this is not it. Change the Log Category back to "Default", native images are not your problem.
This error can also occur if you have a 64 bit dependency while your process or IIS runs on 32 bit.

Simple Error handling in windows phone sdk

I have created a simple app which will do some calcualtions when user input some values.The app is working properly but when user forget to input some values in a textbox and run the app; the app stops working and it hangs. I want to know is there any simple error handling script for windows phone sdk.
I have used this error handiling script before in visual basic and in their its work perfectely.
On Error GoTo error_handler
Dim one,two ....
//some codes
.
.
......
error_handler:
Textblock1.Text = "Error"
I tried to use this above script in sdk but its showing this error
Requested operation is not available because the runtime library function 'Microsoft.VisualBasic.CompilerServices.ProjectData.CreateProjectError' is not defined.
As i asked in the question i was looking for a simple error handling script. I have found "accidently" the error handling script using try and catch method.Below is the code i used for error handiling.
Try
// Your code for app
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Error!", MessageBoxButton.OK)
End Try

Scheduled Console App Crashes with App.config

I have a C# console application that reads app settings from its app.config. The console application is targeted as the action of a Windows Scheduler Task. It runs once a day.
Since adding the code to read from the configuration file the app crashes only when run by the Task Scheduler. When run manually (from command prompt or by clicking on it in it's folder)), the application runs with no issues and it works exactly as expected.
Here's the code that reads from the app settings section:
int someValue = 1;
try
{
if (ConfigurationManager.AppSettings["someValue"] != null)
someValue = int.Parse(ConfigurationManager.AppSettings["someValue"].ToString());
}
catch(Exception exception)
{
// Write to error log
}
This is the exception I'm getting in my error log:
Exception: The type initializer for '<my application name>' threw an exception.
System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: The parameter 'exePath' is invalid.
Parameter name: exePath ---> System.ArgumentException: The parameter 'exePath' is invalid.
Parameter name: exePath
I have tried allowing the task to run with highest privileges. I have tried accessing the configuration file in different ways (Configuration.OpenConfigurationExe(...)), etc... I get the same problem every time. I have tried putting my .exe in various locations on disk. This one is in C:\Program Files. I have looked around on S.O. and this post suggests that it might be a read permissions issue, but if the task runs under my domain's account and has the highest privileges, is that still possible? Note that it runs just fine if I run it manually from a command prompt or by clicking on it.
How can I reliably get a console application to read its app.config if it has been configured to run under Windows Task Scheduler?
Solved it. Turns out I had some legacy bits running code like this:
Configuration config = ConfigurationManager.OpenExeConfiguration("<app exe name>");
This where the exception was being thrown, not in the code in my question. Shame on me. Should have debugged that more thoroughly. I removed it all and replaced the local config object usage above with calls to:
ConfigurationManager.AppSettings["someValue"];
It could definitely be a permission issue, and maybe more than just read permissions. Things to check:
IS the task running under YOUR account, or another account? If this app is stored under a directory in your local profile then even other administrator accounts don't have permissions by default (they have to manually set permissions). Try moving the application to a local folder on the hard drive and set permissions as needed.
Try setting audit settings for the directory your app is stored in. If it is a permission issue then the security log will tell you more about what and why it is getting denied.

How to handle UnauthorizedAccessException from User Code

In a Windows Application I have, I am doing some changes in registry like deleting a particular key, in some test scenarios like in a Vista machine with its UAC put on, I'm getting System.UnauthorizedAccessException. My code would look something like this:
try
{
//delete registry keys
}
catch (UnauthorizedAccessException ex)
{
//handling
}
catch (Exception genEx)
{
//handling
}
But the application would still go crashing., not being handled by the catch block. Is there some way I could handle it?
You're probably throwing another exception from the catch block.
Try commenting all lines in the catch block and it should work just fine.
Most common and obvious reason is that the path / file program is trying access doesn't have access to the identity under which its running.
Read more here on MSDN
you have to set admin access for your application
for that just right click and select properties
and check Run as Administrator check and run your application
to run as administrator for all users click on all user button and select run as administrator for all users this will always run your application with admin access for all users

Categories