三、将图片保存到一个XML文件
WinForm的资源文件中,将PictureBox的Image属性等非文字内容都转变成文本保存,这是通过序列化(Serialization)实现的,
例子:
| using System.Runtime.Serialization.Formatters.Soap; Stream stream = new FileStream("E:\\Image.xml",FileMode.Create,FileAccess.Write,FileShare.None); SoapFormatter f = new SoapFormatter(); Image img = Image.FromFile("E:\\Image.bmp"); f.Serialize(stream,img); stream.Close(); |
四、屏蔽CTRL-V
在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:
| private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Control && e.KeyCode==Keys.V) e.Handled = true; } |
五、判断文件或文件夹是否存在
使用System.IO.File,要检查一个文件是否存在非常简单:
| bool exist = System.IO.File.Exists(fileName); |
如果需要判断目录(文件夹)是否存在,可以使用System.IO.Directory:
| bool exist = System.IO.Directory.Exists(folderName); |

