如何在 C# 中将字符串转换为 DWORD

本文关键字:字符串 转换 DWORD | 更新日期: 2023-09-27 18:33:48

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

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

如何在 C# 中将字符串转换为 DWORD

考虑改用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
}