C# 制作使用.csv文件的web表单转换器
我正在写一个网络表单,可以将缩写转换成它们的实际含义。我使用的是一个.csv文件,两列之间用“;”分隔(如果需要,这可以改变)。第一列是用户输入(缩写),第二列是输出(意思)。我对C#相当陌生,所以我有点挣扎,但我认为我在正确的道路上C# 制作使用.csv文件的web表单转换器,c#,webforms,streamreader,C#,Webforms,Streamreader,我正在写一个网络表单,可以将缩写转换成它们的实际含义。我使用的是一个.csv文件,两列之间用“;”分隔(如果需要,这可以改变)。第一列是用户输入(缩写),第二列是输出(意思)。我对C#相当陌生,所以我有点挣扎,但我认为我在正确的道路上 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebCon
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
private Dictionary<string, string> _dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // let's ignore case when comparing.
protected void Page_Load(object sender, EventArgs e)
{
using (var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
{
while (!reader.EndOfStream)
{
string[] tokens = reader.ReadLine().Split(';');
_dictionary[tokens[0]] = tokens[1];
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string output;
if(_dictionary.TryGetValue(TextBox1, out TextBox2))
return TextBox2;
throw new Exception("Input not recognised");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.IO;
命名空间WebApplication1
{
公共部分类WebForm1:System.Web.UI.Page
{
private Dictionary _Dictionary=new Dictionary(StringComparer.OrdinalIgnoreCase);//比较时忽略大小写。
受保护的无效页面加载(对象发送方、事件参数e)
{
使用(var reader=newstreamreader(File.OpenRead(@“C:/dictionary.csv”))
{
而(!reader.EndOfStream)
{
string[]tokens=reader.ReadLine().Split(“;”);
_字典[tokens[0]]=tokens[1];
}
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
字符串输出;
if(_dictionary.TryGetValue(TextBox1,out TextBox2))
返回文本框2;
抛出新异常(“未识别输入”);
}
}
}
}
非常感谢您的帮助。webform由输入(textbox1)、按钮和输出(textbox2)组成
为清晰起见编辑
这不会运行…我认为这不是一个好的形式,但对于您的简单使用,它应该可以工作:
protected void Button1_Click(object sender, EventArgs e)
{
string output;
if (_dictionary.TryGetValue(TextBox1.Text, out output))
{
TextBox2.Text = output;
}
else
{
TextBox2.Text = "Input not recognised";
}
}
那么问题是什么呢?这是一个更大的应用程序的一部分吗?我有点困惑,为什么要用C来做这件事,而不是用一些更便于移植的东西,比如带有JavaScript的HTML文件。注:看起来你应该使用
TryGetVale(TextBox1.Text,out output)
@BernhardHofmann我被要求用C#来做这件事,它将成为web服务的一部分