Ich habe eine WPF C# -Anwendung. Ich brauche es, um 'Produkte' speichern zu können. Diese Produkte haben einen Produktnamen, einen Kundennamen und einen Firmwarestand. Dies ist mein aktueller Code zum Speichern und Laden, aber es funktioniert nicht. Ich denke, einen anderen Ansatz, um es zu versuchen, alle zusammen:Einfachste Möglichkeit zum Speichern und Laden von Informationen 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;
}
}
}
"_wie auch immer es funktioniert_" Warum es nicht funktioniert? –
Definieren Sie "nicht funktioniert." Kannst du genauer sein? Was erwarten Sie und was passiert eigentlich? Warum benutzen Sie nicht einfach einen der Standard-.NET-Serialisierer/Deserialisierer? Warum erfinden Sie das Rad neu? – EJoshuaS
Der obige Code hat nichts mit WPF zu tun. – mechanic