У меня есть приложение WPF C#. Мне нужно это, чтобы иметь возможность сохранять «Продукты». Эти продукты будут иметь имя продукта, имя клиента и местоположение прошивки. Это мой текущий код для сохранения и загрузки, но он не работает. Я имею в виду попробовать другой подход ко всему этому вместе:Самый простой способ сохранить и загрузить информацию C#
public class Product
{
private string productName;
private string customerName;
private string firmwareLocation;
public string getProductName()
{
return productName;
}
public bool setProductName(string inputProductName)
{
productName = inputProductName;
return true;
}
public string getCustomerName()
{
return customerName;
}
public bool setCustomerName(string inputCustomerName)
{
customerName = inputCustomerName;
return true;
}
public string getFirmwareLocation()
{
return firmwareLocation;
}
public bool setFirmwareLocation(string inputFirmwareLocation)
{
inputFirmwareLocation = firmwareLocation;
return true;
}
public Product(string inProductName, string inCustomerName, string inFirmwareLocation)
{
inProductName = productName;
inCustomerName = customerName;
inFirmwareLocation = firmwareLocation;
}
public void Save(TextWriter textOut)
{
textOut.WriteLineAsync(productName);
textOut.WriteLineAsync(customerName);
textOut.WriteLineAsync(firmwareLocation);
}
public bool Save(string filename)
{
TextWriter textOut = null;
try
{
textOut = new StreamWriter(filename);
Save(textOut);
}
catch
{
return false;
}
finally
{
if (textOut != null)
{
textOut.Close();
}
}
return true;
}
public static Product Load (string filename)
{
Product result = null;
System.IO.TextReader textIn = null;
try
{
textIn = new System.IO.StreamReader(filename);
string productNameText = textIn.ReadLine();
string customerNameText = textIn.ReadLine();
string firmwareLocationText = textIn.ReadLine();
result = new Product(productNameText, customerNameText, firmwareLocationText);
}
catch
{
return null;
}
finally
{
if (textIn != null) textIn.Close();
}
return result;
}
}
}
«Как бы он не работал» «Почему он не работает? –
Определите «не работает». Можете быть более конкретными? Что вы ожидаете и что на самом деле происходит? Кроме того, почему вы не используете один из стандартных сериализаторов/десериализаторов .NET для этого? Зачем изобретать колесо? – EJoshuaS
Код выше не имеет ничего общего с WPF. – mechanic