【发布时间】:2016-01-01 22:10:29
【问题描述】:
我正在尝试设置我的表单以从 .txt 文件中列出的路径加载其 BackgroundImage。
文本文件的内容如下所示:
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Application Name", "Skins", "Background.png")
当表单加载时,我运行以下代码:
//LOAD FORM
string BackgroundSkinsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Application Name", "Skins", "Skin.cfg");
this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
用户可以通过单击与不同背景图像相关的按钮来更改该文本文件的内容:
//CHANGE BG IMAGE
private void ChangeBGButton_Click(object sender, EventArgs e)
{
string BackgroundSkinsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Application Name", "Skins", "Skin.cfg");
System.IO.File.WriteAllText(BackgroundSkinsPath, "System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Application Name", "Skins", "Background2.png")");
this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
}
我想将背景图像位置写入文件的原因是,下次用户加载应用程序时,它将具有他们选择的背景图像,这是我能想到的唯一方法这样做。
问题是,我写的代码不起作用,我不知道如何修复它。
我希望用户能够从“我的文档”文件夹中的文件夹中为应用程序选择背景图像(通过按钮或类似方式),并让应用程序记住他们下次启动时选择的图像应用程序。
【问题讨论】:
标签: c# string image file background