C# 返回值为布尔值的方法
我有下面的代码,其中ShellExecuteEx在执行时返回布尔值true或false。通过将其转换为字符串,我将其分配给一个类级变量C# 返回值为布尔值的方法,c#,interop,user32,C#,Interop,User32,我有下面的代码,其中ShellExecuteEx在执行时返回布尔值true或false。通过将其转换为字符串,我将其分配给一个类级变量 strShellCallStatus = ShellExecuteEx(ref info).ToString(); [DllImport("shell32.dll", CharSet = CharSet.Auto)] static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo); pub
strShellCallStatus = ShellExecuteEx(ref info).ToString();
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
public static void exev()
{
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb = "open";
info.lpFile = "c:\\windows\\notepad.exe";
info.nShow = 5;
info.fMask = 0x440;
info.hwnd = IntPtr.Zero;
strShellCallStatus = ShellExecuteEx(ref info).ToString();
}
我应该担心ShellExecuteEx返回空值吗?如果是这样,我想使用以下语句:
strShellCallStatus = Convert.ToString(ShellExecuteEx(ref info));
只要
ShellExecuteEx
签名不是bool?ShellExecuteEx()
,您不应该害怕它会返回null
,因为bool
是一个默认值类型false
简单-签名为
bool ShellExecuteEx()
的方法无法返回null
,因为它甚至无法编译。一个bool
怎么可能是null
?为什么需要返回null值,要么应该执行,要么不执行。