当我们将txt文件添加到程序中时,需要读取里面的数据并使用,我的思路是先确定txt文件中的路径,在使用streamreader关键字来读取数据并使用。首先txt已经添加到程序中,我使用这种方法来获取文件路径
string tpath=System.IO.path.GetDirectoryName(System.Reflection.ASSembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName)+"\\ConfigText.txt";
或者是System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName) + @"\ConfigText.txt";
这两种方法来获取文件路径。
若是确定路径没错则不用判断路径是否存在,否则
public class name
{
public string ApiName { get; set; }
/// <summary>
/// 接口地址
/// </summary>
public string ApiServerHost { get; set; }
/// <summary>
/// 接口端口
/// </summary>
public string ApiServerPort { get; set; }
/// <summary>
/// 获取机台信息
/// </summary>
public string API_GetMacInfo { get; set; }
/// <summary>
/// 原料入库接口
/// </summary>
public string API_MaterialIn { get; set; }
/// <summary>
/// 获取人员信息接口
/// </summary>
public string API_GetStaffInfo { get; set; }
/// <summary>
/// 通过工装查找周转卡信息接口
/// </summary>
public string API_ToolCardQuery { get; set; }
/// <summary>
/// 新条码查询机台明细
/// </summary>
public string API_queryMachineTest { get; set; }
/// <summary>
/// 选定的工厂种类
/// </summary>
public string PlantType { get; set; }
/// <summary>
/// 全部工厂种类
/// </summary>
public string AllPlantType { get; set; }
/// <summary>
/// 更新周转卡接口
/// </summary>
public string API_UpdateTurnCard { get; set; }
/// <summary>
/// 获取胶料周转卡信息接口
/// </summary>
public string API_GetRubberTurnCard { get; set; }
/// <summary>
/// 通过周转卡编号查找周转卡信息接口
/// </summary>
public string API_TurnOverCardQuery { get; set; }
/// <summary>
/// 胶料消耗接口
/// </summary>
public string API_RubberTurnCardConsume { get; set; }
/// <summary>
/// 机台实验绑定接口
/// </summary>
public string API_bindingMachineTest { get; set; }
/// <summary>
/// 轮胎实验绑定接口
/// </summary>
public string API_bindingNewBarCode { get; set; }
/// <summary>
/// 获取搬运任务列表接口
/// </summary>
public string API_GetCarryTaskList { get; set; }
/// <summary>
/// 预约搬运单(抢单)接口
/// </summary>
public string API_GetCarryTask { get; set; }
/// <summary>
/// 更新搬运单接口
/// </summary>
public string API_CarryTaskUpdate { get; set; }
/// <summary>
/// 库位更新接口
/// </summary>
public string API_StorageLocUpdate { get; set; }
/// <summary>
/// 更新工装接口
/// </summary>
public string API_StToolsUpdate { get; set; }
/// <summary>
/// 库位查询接口
/// </summary>
public string API_StockQuery { get; set; }
/// <summary>
/// 出入库接口
/// </summary>
public string API_InOut { get; set; }
/// <summary>
/// 盘库接口
/// </summary>
public string API_CheckStock { get; set; }
/// <summary>
/// 钢丝查询接口
/// </summary>
public string API_QuerySteel { get; set; }
/// <summary>
/// 钢丝消耗接口
/// </summary>
public string API_SteelUse { get; set; }
}
}
public name getmessage(string path)
{
using(steamread sd=new streamread(path,encoding.default))//若txt文件中出现中文则需要encoding.default,否则可以不写,这句话的意思就是创建一个读取流读取数据
{
streambulider sb=new streambuilder()//创建实例,若配置文件这种使用streambuilder,和string差不多,请自己去看两者区别
{
while(!sd.endofstream)//判断是否读完
{
sb.append(sd.readline());//将txt数据添加到sb中,其数据类型是stringbulider
}
return sb.tosring.toobj<name>();//类型转换请借鉴jsonhelper百度就有
}
这是我txt文件的内容,则可以改变配置文件的来改变url,不用通过程序去改变。
"ApiName":"jfly.openapi","ApiServerHost" : "171.188.96.88","ApiServerPort" : "80","PlantType":"半制品","AllPlantType":"半制品,成型,钢丝","API_GetMacInfo" : "machine/query/v1","API_MaterialIn" : "job/materialIn/v1","API_GetStaffInfo" : "base/userQuery/v1","API_ToolCardQuery" : "job/toolTCardQuery/v1","API_UpdateTurnCard" : "job/turnoverCardUpdate/v1","API_TurnOverCardQuery" : "job/turnoverCardQuery/v1" ,"API_GetRubberTurnCard" : "matrk/rubber/v1","API_bindingMachineTest":"quality/bindingMachineTest/v1" ,"API_queryMachineTest":"quality/queryMachineTest/v1" ,"API_bindingNewBarCode":"quality/bindingNewBarCode/v1" ,"API_RubberTurnCardConsume" : "matrk/rubberChild/v1","API_GetCarryTaskList" : "pad/carryTaskList/v1","API_GetCarryTask" : "pad/carryTaskGet/v1","API_CarryTaskUpdate" : "pad/carryTaskUpdate/v1","API_StorageLocUpdate" : "pad/storageLocUpdate/v1","API_StToolsUpdate" : "pad/stToolsUpdate/v1","API_StockQuery" : "pad/stockQuery/v1","API_InOut" : "pad/inout/v1","API_CheckStock" : "pad/checkStock/v1","API_QuerySteel":"job/findGsStock/v1","API_SteelUse":"job/materialOut/v1"