保存和加载信息的最简单方法c#

时间:2016-08-04 14:59:55

标签: c# serialization save loading

我有一个WPF 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;
    }
}

}

1 个答案:

答案 0 :(得分:2)

有点不清楚你的意思是什么"没有工作"但我建议您只使用标准的.NET序列化/反序列化库,而不是尝试重新发明轮子。这里没有必要做任何自定义的事情。请参阅以下内容:https://msdn.microsoft.com/en-us/library/mt656716.aspx

作为旁注,为什么使用getX()和setX()方法而不是属性?它不是标准的C#。例如,以下内容:

private string productName;

public string getProductName()
{
    return productName;
}

public bool setProductName(string inputProductName)
{
    productName = inputProductName;
    return true;
}

应该是

public string ProductName
{
    get;
    set;
}

我猜测你的代码不起作用的原因之一是它有多个炫目的竞争条件。例如,你的所有3个写操作都是异步的,并且一个接一个地发出;当你开始下一个时,我们无法保证完成前一个。我甚至不清楚你是否保证以特定的顺序编写这些行(你的反序列化逻辑就是这种情况)。它也完全可能(实际上可能)你在写操作过程中关闭文件。

我还建议使用"使用"阻止文件流。