我正在尝试将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);
}
}