如何在C#中将String转换为DWORD

时间:2014-12-09 05:22:05

标签: c#

当我尝试将字符串转换为DWORD时,它不会转换并且异常即将到来:输入字符串未处于正确格式

string cData = File.ReadAllText(@file1Text.Text );
DWORD rData = Convert.ToUInt32(cData);

1 个答案:

答案 0 :(得分:1)

请考虑使用TryParse。它不会抛出异常,如果它通过,那么你将把值存储在你的局部变量中:

string cData = File.ReadAllText(@file1Text.Text );
DWORD rData;

if (UInt32.TryParse(cData, out rData))
{
    // If you get here, the data was valid and is now stored in rData
}
else
{
    // If you get in here, the cData was not a valid UInt32
}