如何将对象/用户设置保存到CSV文件 [英] How to save object/user settings to CSV file

查看:90
本文介绍了如何将对象/用户设置保存到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序通过映射到对象的用户输入表单接收用户输入.我需要能够将结果(对象)保存到CSV文件,并且当用户重新加载我的应用程序时,我需要csv文件(我的对象)的结果来填充我的输入表单.

有人能为我指出正确的方向吗?

预先感谢,
-DA

I have an application that receives user input via a user input form that''s mapped to an object. I need to be able to save the results(object) to a CSV file and when the user reloads my app I need the results of the csv file(my object) to populate the my input form.

Can someone please point me in the right direction on how to go about achieving this.

Thanks in advance,
-DA

推荐答案

在XML文件中创建设置后,您可以使用CodePlex(Microsoft开放源代码)工具将其转换为CSV文件http://xmltocsv.codeplex.com/ [ ^ ]
After creating the setting in XML file, you can use CodePlex (Microsoft Open source) tool to covert into CSV file http://xmltocsv.codeplex.com/[^]


以下是用于保存和检索应用程序设置的示例代码

Here is the sample code to save and retrieve application settings

Public Class AppSettings
   Private _configFileName As String 'local var to hold the config file name
   Private _configFileType As Config 'local var to hold the config file type (private or shared)

   'config file options
   Public Enum Config
      SharedFile  'all users use the same config file
      PrivateFile 'each user has their own config file
   End Enum

   'constructor
   Public Sub New(ByVal ConfigFileType As Config)
      _configFileType = ConfigFileType 'remember this setting

      InitializeConfigFile() 'setup the filename and location
   End Sub

   'initialize the apps config file, create it if it doesn't exist
   Private Sub InitializeConfigFile()
      Dim sb As New Text.StringBuilder

      'build the path\filename depending on the location of the config file
      Select Case _configFileType
         Case Config.PrivateFile 'each user has their own personal settings
            'use "\documents and settings\username\application data" for the config file directory
            sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
         Case Config.SharedFile 'all users share the same settings
            'use "\documents and settings\All Users\application data" for the config file directory
            sb.Append(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
      End Select

      'add the product name
      sb.Append("\")
      sb.Append(Application.ProductName)

      'create the directory if it isn't there
      If Not IO.Directory.Exists(sb.ToString) Then
         IO.Directory.CreateDirectory(sb.ToString)
      End If

      'finish building the file name
      sb.Append("\")
      sb.Append(Application.ProductName)
      sb.Append(".config")

      _configFileName = sb.ToString 'completed config filename

      'if the file doesn't exist, create a blank xml
      If Not IO.File.Exists(_configFileName) Then
         Dim fn As New IO.StreamWriter(IO.File.Open(_configFileName, IO.FileMode.Create))
         fn.WriteLine("")
         fn.WriteLine("<configuration>")
         fn.WriteLine("  <appsettings>")
         fn.WriteLine("    <!--   User application and configured property settings go here.-->")
         fn.WriteLine("    <!--   Example: <add key=""settingName"" value=""settingValue""/> -->")
         fn.WriteLine("  </appsettings>")
         fn.WriteLine("</configuration>")
         fn.Close() 'all done
      End If
   End Sub

   'get an application setting by key value
   Public Function GetSetting(ByVal key As String) As String
      'xml document object
      Dim xd As New Xml.XmlDocument

      'load the xml file
      xd.Load(_configFileName)

      'query for a value
      Dim Node As Xml.XmlNode = xd.DocumentElement.SelectSingleNode( _
                                "/configuration/appSettings/add[@key=""" & key & """]")

      'return the value or nothing if it doesn't exist
      If Not Node Is Nothing Then
         Return Node.Attributes.GetNamedItem("value").Value
      Else
         Return Nothing
      End If
   End Function

   'save an application setting, takes a key and a value
   Public Sub SaveSetting(ByVal key As String, ByVal value As String)
      'xml document object
      Dim xd As New Xml.XmlDocument

      'load the xml file
      xd.Load(_configFileName)

      'get the value
      Dim Node As Xml.XmlElement = CType(xd.DocumentElement.SelectSingleNode( _
                                         "/configuration/appSettings/add[@key=""" & _
                                         key & """]"), Xml.XmlElement)
      If Not Node Is Nothing Then
         'key found, set the value
         Node.Attributes.GetNamedItem("value").Value = value
      Else
         'key not found, create it
         Node = xd.CreateElement("add")
         Node.SetAttribute("key", key)
         Node.SetAttribute("value", value)

         'look for the appsettings node
         Dim Root As Xml.XmlNode = xd.DocumentElement.SelectSingleNode("/configuration/appSettings")

         'add the new child node (this key)
         If Not Root Is Nothing Then
            Root.AppendChild(Node)
         Else
            Try
               'appsettings node didn't exist, add it before adding the new child
               Root = xd.DocumentElement.SelectSingleNode("/configuration")
               Root.AppendChild(xd.CreateElement("appSettings"))
               Root = xd.DocumentElement.SelectSingleNode("/configuration/appSettings")
               Root.AppendChild(Node)
            Catch ex As Exception
               'failed adding node, throw an error
               Throw New Exception("Could not set value", ex)
            End Try
         End If
      End If

      'finally, save the new version of the config file
      xd.Save(_configFileName)
   End Sub
End Class


这篇关于如何将对象/用户设置保存到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆