如何在这个保存和加载文件的C#脚本中使用类?
如何在这个保存和加载文件的C#脚本中放置类。我目前正在学习C#。我必须用C#为学校制作一个项目,它使用类加载和保存文件。我学会了如何使用类,但当我不得不在脚本中使用它时,我真的感到困惑:如何在这个保存和加载文件的C#脚本中使用类?,c#,visual-studio,C#,Visual Studio,如何在这个保存和加载文件的C#脚本中放置类。我目前正在学习C#。我必须用C#为学校制作一个项目,它使用类加载和保存文件。我学会了如何使用类,但当我不得不在脚本中使用它时,我真的感到困惑: private void button1_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogR
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK) ;
{
StreamReader read = new StreamReader(File.OpenRead(open.FileName));
textBox1.Text = read.ReadToEnd();
read.Dispose();
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamWriter write = new StreamWriter(File.Create(save.FileName));
write.Write(textBox1.Text);
write.Dispose();
}
}
我认为你练习的重点是把责任分开 每个班级只能完成一项任务。 根据这一原则,在逻辑上将程序分为三个级别(UI、业务和数据层)是一种很好的做法 关于这一点有很多要讨论的,所以我建议您看看坚实的原则和三层体系结构 为了帮助您,我建议您按如下方式更改代码(本例使用三个级别,但如果您使用两个级别,就足够了,您的老师不会怀疑您收到了提示)
为什么要麻烦?查看
System.File
。特别是,ReadAllText
和writealtext
方法。如果要将类保存/加载为文件。您可以查看XMLReader。看看这个:您正在该代码中使用类-OpenFileDialog
,StreamReader
,文件
。等等都是班级。此外,这里包含的所有代码都必须在其自己的类中(我猜它可能被称为Form1
)。但是,是否要求必须创建新类才能将文件加载到文本框中/将文件从文本框保存到文件中?
// You User Interface, should use Business level classes (not data)
class YourForm
{
private readonly YourService _myLogicService;
public YourForm()
{
_myLogicService = new YourService(new YourFilePersistor());
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK) ;
{
textBox1.Text = _myLogicService.Read(open.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
var toSave = textBox1.Text;
SaveFileDialog save = new SaveFileDialog();
if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
_myLogicService.Write(save.FileName, toSave);
}
}
}
// Data Layer: this class has dependencies from file system
// (in future a database can be used but you will not change your business service, just implement the interface)
public class YourFilePersistor : IPersistor
{
public string Read(string filePath)
{
return System.IO.File.ReadAllText(filePath); // Or your code
}
public void Write(string filePath, string fileContent)
{
System.IO.File.WriteAllText(filePath, fileContent); // Or your code
}
}
public interface IPersistor
{
string Read(string filePath);
void Write(string filePath, string fileContent);
}
// Service is your "Business Level" for put your application logic on your domain data
// Business classes uses intefaces to abstract data layer and work without dependencies from components like database, remote apis and so on...
public class YourService
{
private readonly IPersistor _repository;
public YourService(IPersistor repository)
{
_repository = repository;
}
public string Read(string filePath)
{
var data = _repository.Read(filePath);
return data;
}
public void Write(string filePath, string fileContent)
{
var data = fileContent;
// here you could do some logic i.e. to validate your data
if (string.IsNullOrWhiteSpace(fileContent))
{
throw new InvalidOperationException("Data is null");
}
// ---
_repository.Write(filePath, data);
}
}