Вопрос

I am trying to run this inline if statement

pRun.StartInfo.FileName = File.Exists("C:\\Test\\Data\\TestingPurposes\\" + UserName 
+ "DailyInfo") ? pRun.StartInfo.UseShellExecute = true : MessageBox.Show("Please 
verify that this file exists");

But I am getting a compile error of:

Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and 'System.Windows.Forms.DialogResult'

How can I remove this error and run the below statement? What I want to do is check if the file exists, if it does, open it. If it does not then produce the messagebox with said message.

Это было полезно?

Решение

In order to use ternary operator, both statement should return the same type or one type should be convertible to other.See documentation:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

You need to use simple if statements intead, it is also more readable:

if(File.Exists("C:\\Test\\Data\\TestingPurposes\\" + UserName + "DailyInfo"))
{ 
   pRun.StartInfo.UseShellExecute = true   
}
else MessageBox.Show("Please verify that this file exists");
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top