C#的读取ini文件单元测试
2020-12-05 本文已影响0人
冀望的air
多的也不说了,直接上代码,如果对于基础的断言什么不了解可以看我之前的C/C++单元测试文章。
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string FilePath = @"这里写你自己的ini文件地址";
string Section = "Number";
string Key = "number";
string KeyOfSampleName = "Input";
string KeyOfSampleName1 = "Output";
int thenum = GetPrivateProfileInt(Section, Key, 0, FilePath);
for (int index = 1; index <= thenum; index++)
{
//解析编号
string ExamStr = index.ToString();
//解析字符串Intput后面的内容
string s = Read(ExamStr, KeyOfSampleName, "", FilePath);
string[] condition = { " " };
//如果在拆分时不需要包含空字符串,则可以使用 StringSplitOptions.RemoveEmptyEntries 选项
//例如在上例中将 StringSplitOptions.None 更改成 StringSplitOptions.RemoveEmptyEntries
string[] result = s.Split(condition, StringSplitOptions.None);//以空格拆分
byte[] byteArray = new byte[result.Length];
for (int i = 0; i < result.Length; i++)
{
//将16进制的string转换为byte
byteArray[i] = byte.Parse(result[i], System.Globalization.NumberStyles.HexNumber);
}
//解析字符串Output后面的内容
string output = Read(ExamStr, KeyOfSampleName1, "", FilePath);
string[] resultOutput = output.Split(condition, StringSplitOptions.None);
byte[] byteArray1 = new byte[resultOutput.Length];
for (int i = 0; i < resultOutput.Length; i++)
{
byteArray1[i] = byte.Parse(resultOutput[i],System.Globalization.NumberStyles.HexNumber);
}
//断言
Assert.IsTrue(true);
}
}
/// <summary>
/// 为INI文件中指定的节点取得整数
/// </summary>
/// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
/// <param name="lpKeyName">欲获取的项名</param>
/// <param name="nDefault">指定的项没有找到时返回的默认值</param>
/// <param name="lpFileName">INI文件完整路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
/// <summary>
/// 为INI文件中指定的节点取得字符串
/// </summary>
/// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
/// <param name="lpKeyName">欲获取的项名</param>
/// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
/// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param>
/// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
/// <param name="lpFileName">INI文件完整路径</param>
/// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
/// <summary>
/// 读取INI文件值
/// </summary>
/// <param name="section">节点名</param>
/// <param name="key">键</param>
/// <param name="def">未取到值时返回的默认值</param>
/// <param name="filePath">INI文件完整路径</param>
/// <returns>读取的值</returns>
public static string Read(string section, string key, string def, string filePath)
{
StringBuilder sb = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, sb, 1024, filePath);
string s = sb.ToString();
return s;
}
}
}