1

我正在尝试将m_settings变量从 volatile 移动到持久记录。我尝试将[serializable]属性添加到类并m_settings使用 BinaryFormatter 将变量发送到文件流,但我收到一条错误消息the file cannot be written, access to the file is denied。我究竟做错了什么?

[Serializable] 
public class SettingsComponent : GH_Component
{
    public SettingsComponent(): base("LoadSettings", "LoadSettings", "Loading ini", "Extra", "Silkworm") { }


    public override void CreateAttributes()
    {
        m_attributes = new SettingsComponentAttributes(this);
    }


    string m_settings_temp;
    string[] m_settings;
    public void ShowSettingsGui()
    {
        var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" };
        if (dialog.ShowDialog() != DialogResult.OK) return;

        m_settings_temp = File.ReadAllText(dialog.FileName);
        m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        ExpireSolution(true);
    }



    protected override void SolveInstance(IGH_DataAccess DA)
    {
        if (m_settings == null)
        {
           AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
           return;
        }
        else
        {  
                    FileStream fs = new FileStream("DataFiletemp.dat", FileMode.Create);
                    BinaryFormatter formatter = new BinaryFormatter();
                    try
                    {
                        formatter.Serialize(fs, m_settings);
                    }
                    catch (SerializationException e)
                    {
                        Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                        throw;
                    }
                    finally
                    {
                        fs.Close();
                    }

                   DA.SetDataList(0, m_settings);
        }  

    }
4

1 回答 1

1

Arthur,如果您的 m_settings 对象并不复杂,您可以使用项目设置(应用程序或用户级别)。以下是如何将一些项目设置保存到 app.config 的示例代码:

     [YourNamespace].Properties.Settings.Default["MyProperty"] = "Demo Value";
     [YourNamespace].Properties.Settings.Default.Save();

如果您需要二进制序列化,您可以使用如下代码:(序列化对象和继承对象必须标记为可序列化)

    /// <summary>
    /// Serializes object to file
    /// </summary>
    /// <param name="data"></param>
    /// <param name="FilePath"></param>
    public static void SerializeMyObject(object data, string FilePath)
    {
        System.IO.Stream stream = null;
        try
        {
            stream = System.IO.File.Open(FilePath, System.IO.FileMode.Create);
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter =
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            bformatter.Serialize(stream, data);
            stream.Close();
            stream.Dispose();
        }
        catch (Exception ex)
        {
            try
            {
                stream.Close();
                stream.Dispose();
            }
            catch (Exception)
            {
            }
            throw new Exception(ex.Message);
        }
    }
于 2012-09-15T14:17:31.367 回答