我想实现这样一个功能:
我在一个textbox中显示一个路径的名称:假设是c:\myname\
我想在我不手动改变它的情况下,每次启动程序时,都是显示这个路径。
如果某一次启动后,我改成d:\yours\,那从这次后的每次启动都显示d:\yours\
请问:不通过读写数据库,能实现这样的功能吗?如果能该如何实现?

解决方案 »

  1.   

    你用OpenFileDialog.InitialDirectory属性设置初始路径
    再用OpenFileDialog.RestoreDirectory属性设置关闭的时候是否还原目录就行了把OpenFileDialog的路径存入textbox.text里就可以了
      

  2.   

    它的属性EnableViewState只有保留当前页面,如果程序启动,肯定丢失.
    如果不放数据库,那只有存放到文件中.代码非常简单读写文件:public ReadFile(string strFileName)
    {
      StreamReader sr = new StreamReader(strFileName),System.Text.Encoding.Default);
      while (sr.Peek() >= 0) 
      {
         Console.WriteLine(sr.ReadLine());
      }
      sr.Close();  
    }Public Shared Sub WriteFile(ByVal strLog As String, ByVal page As System.Web.UI.Page)       
            If Directory.Exists(page.Server.MapPath("LogFolder")) = False Then
                Directory.CreateDirectory(page.Server.MapPath("LogFolder"))
            End If
            Dim pw As StreamWriter = New StreamWriter(page.Server.MapPath(".") & "\LogFolder\log.txt", True, System.Text.Encoding.UTF8)
            pw.WriteLine(strLog)
            pw.Flush()
            pw.Close()
        End Sub
      

  3.   

    可以使用xml文件保持你要的信息
      

  4.   

    选择合适的存储容器。
    txt,ini,xml 文件等等。
      

  5.   

    可以放在注册表中,配置文件中 都很简单方便.在配置文件中的方法(app.config 或web.config)
    //读
    public static string GetValue(string AppKey)
    {
    try
    {
    string AppKeyValue;
    AppKeyValue=System.Configuration.ConfigurationSettings.AppSettings.Get(AppKey);
    return AppKeyValue;

    catch(Exception ex)
    {
    throw ex;
    }
    }
    //写
    public static void SetValue(string AppKey,string AppValue)
    {
    //System.Configuration.ConfigurationSettings.AppSettings.Set(AppKey,AppValue);
    XMLDocument xDoc = new XmlDocument();
    xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode;
    XmlElement xElem1;
    XmlElement xElem2;
    xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=''" + AppKey + "'']");
    if ( xElem1 != null ) xElem1.SetAttribute("value",AppValue);
    else
    {
    xElem2 = xDoc.CreateElement("add");
    xElem2.SetAttribute("key",AppKey);
    xElem2.SetAttribute("value",AppValue);
    xNode.AppendChild(xElem2);
    }
    xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
    }
      

  6.   

    我的配置文件就是写在xml里的,但是那是静态的,我只会写静态的,动态的怎么写?
      

  7.   

    首先你在配置文件中 <appSettings>增加节    <add key="FilePath" value="aa">
    读取:TextBox2.Text=System.Configuration.ConfigurationSettings.AppSettings["FilePath"]
    修改:
    if(System.Configuration.ConfigurationSettings.AppSettings["FilePath"]==TextBox2.Text)
    return;
    //打开配置文件)
    //string filename =System.Windows.Forms.Application.ExecutablePath + ".config";//此为winform程序配置文件路径
    string filename =Server.MapPath(@"web.config");//此为webform程序配置文件路径
    XmlDocument  xmldoc= new XmlDocument();
    xmldoc.Load(filename);
    XmlNodeList xNodes=xmldoc.DocumentElement.ChildNodes;
    foreach(XmlElement xe in xNodes)
    {
    if(xe.Name.ToLower()=="appsettings")
    if(xe.ChildNodes.Count>0)
    foreach(XmlElement cxe in xe.ChildNodes)
    if(cxe.Attributes["key"].InnerText=="FilePath")
    cxe.Attributes["value"].Value =TextBox2.Text;
    }
    xmldoc.Save(filename);