6:多个form之间数据共享
由于代码比较多,下面贴出核心代码
|---获取所有可执行文件、初始化xml保存数据。
using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Xml; namespace flip_v2 { /** 图片+Xml工具类*/ public class ImgTool { /** * * 传递一个完整路径,返回一个ilst集合,0为路径(不含文件名) 1:播放的文件名(不包含路径) * 如C:\Users\Public\Pictures\Sample Pictures\八仙花.jpg * 0:C:\Users\Public\Pictures\Sample Pictures * 1:八仙花.jpg */ public static string[] pathToList(string path){ string[] array = new string[2]; int last = path.LastIndexOf("\\"); if(last!=-1){ array[0]=(path.Substring(0,last)); array[1]=(path.Substring(last+1)); } return array; } /**把所有文件封装到map中 key为uint64 value=string*/ public static Dictionary<UInt64,string> pathToMap(string path){ Dictionary<UInt64,string> dict = new Dictionary<UInt64,string>(); DirectoryInfo info = new DirectoryInfo(path); FileSystemInfo[] fileSystem = info.GetFileSystemInfos(); UInt64 count = 1; //获取所有子文件(不包含子文件夹), foreach(FileSystemInfo f in fileSystem){ FileInfo file = f as FileInfo; if(file!=null && fileterEndName(file.Name)) dict.Add(count++,f.FullName); } return dict; } //文件过滤器:png,jpg,gif public static bool fileterEndName(string endName){ endName = endName.ToLower(); bool b = false; string tag = ".png|.jpg|.gif"; int last = endName.LastIndexOf("."); if(last!=-1){ endName = endName.Substring(last); if(tag.Contains(endName)) b= true; } return b; } /**操作xml:默认保存到c:\\property.xml*/ public static void createXml(string path){ //xml样式 XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineOnAttributes = false; //本地创建一个新的xml XmlWriter write = XmlWriter.Create(path,settings); write.WriteStartDocument(); write.WriteStartElement("property"); write.WriteStartElement("time"); write.WriteAttributeString("id","setTime"); write.WriteValue("3000"); write.WriteEndElement(); write.WriteStartElement("localhostPath"); write.WriteAttributeString("id","setInputBoxPath"); write.WriteValue(""); write.WriteEndElement(); write.WriteEndElement(); write.WriteEndDocument(); //刷新缓冲区,并释放资源 write.Flush(); write.Close(); } } }|----目录图片的核心代码
/*执行的事件*/ public void theout(object source, EventArgs e) { Boolean b = checkTag.Checked; if(!b){ myTimer.Stop(); //停止定时器 }else{ //执行获取图片 string pageStr = showImg.Text; UInt64 pageNum = UInt64.Parse(pageStr); pageNum = pageNum+1; try{ showPage(pageNum); }catch(Exception){ //循环 showPage(1); //MessageBox.Show("已是未尾"); } } } /**后退*/ void BackOffClick(object sender, EventArgs e) { string pageStr = showImg.Text; UInt64 pageNum = UInt64.Parse(pageStr); pageNum = pageNum-1; try{ showPage(pageNum); }catch(Exception){ MessageBox.Show("已是顶页"); } } /**前进*/ void ForwardClick(object sender, EventArgs e) { string pageStr = showImg.Text; UInt64 pageNum = UInt64.Parse(pageStr); pageNum = pageNum+1; try{ showPage(pageNum); }catch(Exception){ MessageBox.Show("已是未尾"); } } public bool showPage(UInt64 pageNum) { if(dict==null || dict.Count==0){ MessageBox.Show("抱歉,没有找到可执行文件!"); stopImg.ForeColor=Color.Red; startImg.ForeColor=Color.LimeGreen; /*还原*/ Reduction(); return false; } string showImgPath = dict[pageNum]; showImg.Image = Image.FromFile(showImgPath); //回显信息 string[] str = ImgTool.pathToList(showImgPath); Many.Text = "来自:"+str[0]; Single.Text = "正在浏览【"+str[1]+"】"; //回显页码 showImg.Text = Convert.ToString(pageNum); return true; }下面贴出程序执行图版
![]()
原文:http://blog.csdn.net/hubiao_0618/article/details/21297311