将令牌保存到文件 [英] Saving tokens to a file

查看:108
本文介绍了将令牌保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个名为令牌"的列表,我希望能够保存和加载它们.我希望能够执行以下操作:

hey guys,

i have got a list called ''tokens'' and i want to be able to save and load them. i want to be able to do something like:

if a.startswith("save ")
dim sw as streamwriter = new streamwriter(appdomain.currentdomain.basedirectory & a.substring(5) & ".txt")
sw.write(tokens)
sw.close()



我希望能够做类似的事情,但是没有用.



i want to be able to do something like that, but it doesn''t work. can you please help me?

推荐答案

首先,在不显示其类型定义的情况下询问一些对象标记的问题不是很好.显示未编译的代码也不太好.什么,使用实际代码片段会带来问题吗?复制/粘贴怎么样?

但是,这并不重要,只要您尝试像显示的代码中那样使用解决方案"即可.让我们了解为什么它不起作用.本质上,
First of all, its not nice to ask about your problem with some object tokens without showing the definition of its type. It''s also not nice to show the code which does not compile. What, was the using a fragment of real code a problem? How about copy/paste?

However, it does not matter it you try to use the "solution" like in the code you show. Let''s understand why it cannot work. Essentially,
sw.Write(tokens);


将输出


will output

sw.Write(tokens.ToString());



对于列表,其他容器和大多数其他类型,它将仅输出包含类型名称的字符串.那么,有什么用?

您需要使用某种序列化,这可能是您需要学习的东西.请从这里开始:
http://en.wikipedia.org/wiki/Serialization [ http://msdn.microsoft.com/en-us/library/ms233843.aspx [ ^ ],
http://msdn.microsoft.com/zh-我们/library/system.runtime.serialization%28v=vs.100%29.aspx [ http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. runtime.serialization.datacontractserializer.aspx [ ^ ].

这种方法最易于使用,非常健壮,通用且非侵入式.我在过去的回答中主张采用这种方法:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [



For lists, other containers and most other types it will simply output the string containing the type name; so, what''s the use?

You need to use some kind of serialization, and this is something you will probably need to learn. Please start here:
http://en.wikipedia.org/wiki/Serialization[^],
http://msdn.microsoft.com/en-us/library/ms233843.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization%28v=vs.100%29.aspx[^].

I would strongly recommend to use Data Contract and DataContractSerializer. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^].

This approach is easiest to use, very robust, universal and non-intrusive. I advocate this approach in my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


您可以看到^ ],Write有许多重载,但是它们都不能将列表或集合作为输入.
遍历您的列表并逐行写文本.
As you can see here[^], Write has a number of overloads but none of them can take a list or collection as input.
Loop through your list and write text line by line.


这篇关于将令牌保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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