質問

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