首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#读写txt文件的有关问题

2013-08-11 
c#读写txt文件的问题using Systemusing System.Collections.Genericusing System.Linqusing System.Tex

c#读写txt文件的问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevelopLibrary.Enums;
using DevelopLibrary.DevelopAPI;
using System.IO;


namespace sellopen
{
    public class sell : EventStrategyBase
    {

        FuturesAccount fa = null;//定义一个策略生命周期的变量
        
StreamWriter outfile = null;


        public override void OnStart()//在策略启动时执行,一般用来创建图表,或读取账户信息。
        {
           
            outfile = new StreamWriter(@"e:\out2.txt");
          
        }

       

        public override void OnBarOpen()//如果是tick级的,比onTick事件晚一个tick.如果是其他级别,在一个Bar刚建立的时候执行。
        {
                      
            string str1 = Price + ","+LOW[LOW.Length - 2]+ ","+ "\r\n";
            outfile.WriteLine(str1);
           
         }
         
        
    }
}


请问这个怎么改成txt读写共享模式?就是程序在输出变量到txt时,允许txt被打开。现在好像只能在程序结束时才能打开txt。


然后第二个问题是,麻烦各位大哥帮我把读取txt格式写一下。



[解决办法]
改用log4net吧
[解决办法]
当一个流占用一个文件的时候,是无法被其他资源共享的吧
变通一个解决方案,用一个队列或其他的什么保存修改内容,再定时更新
或者及时关闭流,应该不难吧?
------解决方案--------------------


用 using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(newPath)))
                    {

                    }
先把數據讀取到內存中。然後你愛怎樣就怎樣了。
[解决办法]
New FileStream(file, FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite)
[解决办法]
建议不要一直打开文件 
[解决办法]
New FileStream(。。。).Close()
关闭就行了
[解决办法]
读取txt文件:
List<string> list = new List<string>();
            using (StreamReader sr = File.OpenText(@"e:\\abc.txt"))
            {
                string lineString = sr.ReadLine();
                if (lineString != null)
                {
                    list.Add(lineString);
                    while (lineString != null)
                    {
                        lineString = sr.ReadLine();
                        list.Add(lineString);
                    }
                }
            }
[解决办法]
写入的代码:
double a = 100;
using (TextWriter tw = File.CreateText(@"c:\abc\log.txt"))


{
    tw.WriteLine(a.ToString());
    tw.Flush();
}
[解决办法]

引用:
当一个流占用一个文件的时候,是无法被其他资源共享的吧
变通一个解决方案,用一个队列或其他的什么保存修改内容,再定时更新
或者及时关闭流,应该不难吧?


外部文件用xml

热点排行