如何在循环中将数据写入文件

时间:2017-07-12 07:16:11

标签: c#

我正在开发一个Streaming,它将从Fx3板流式传输数据。

我想在将数据从Streamer写入文件时将数据从Streamer写入PC Memory(files),,数据写入文件 但我希望在第一个文件n内再次找到一些没有文件后写,所以任何人都可以帮我解决这个问题。

if (Save == true)   //Check if save Button is marked
{
    while (true)
    {
        const int sizeLimitInBytes = 360960*16;
        using (var fs = new FileStream("C:\\user\\mady\\desktop\\myData.txt", FileMode.Create, FileAccess.ReadWrite))
        {
            while (true)
            {
                fs.Write(xBufs[k], 0, xBufs[k].Length);
                index++;
                Offset += len;
                EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
                if (fs.Length >= sizeLimitInBytes)
                {
                    fs.Close();
                    using (var fs1 = new FileStream("C:\\user\\mady\\desktop\\myData1.txt", FileMode.Create, FileAccess.ReadWrite))  
                    {
                        while (true)
                        {
                            fs1.Write(xBufs[k], 0, xBufs[k].Length);
                            index++;
                            Offset += len;
                            EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
                            if (fs1.Length >= sizeLimitInBytes)
                            {
                                fs1.Close();
                                using (var fs2 = new FileStream("C:\\user\\mady\\desktop\\myData2.txt", FileMode.Create, FileAccess.ReadWrite))
                                {
                                    while (true)
                                    {
                                        fs2.Write(xBufs[k], 0, xBufs[k].Length);
                                        index++;
                                        Offset += len;
                                        EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
                                        if (fs2.Length >= sizeLimitInBytes)
                                        {
                                            fs2.Close();
                                            using (var fs3 = new FileStream("C:\\user\\mady\\desktop\\myData3.txt", FileMode.Create, FileAccess.ReadWrite))
                                            {
                                                while (true)
                                                {
                                                    fs3.Write(xBufs[k], 0, xBufs[k].Length);
                                                    index++;
                                                    Offset += len;
                                                     EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
                                                     if (fs3.Length >= sizeLimitInBytes)                                               
                                                     {
                                                         //using (var fs = new FileStream("C:\\user\\mady\\desktop\\myData3.txt", FileMode.Create, FileAccess.ReadWrite))
                                                         fs3.Close();
                                                         fs.Write(xBufs[k], 0, xBufs[k].Length);
                                                         index++;
                                                         Offset += len;
                                                         EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]);
                                                     }
                                                }
                                            }
                                        }
                                    }
                                }
                            }                       
                        }
                    }
                }
            }
        } 
    }
}

0 个答案:

没有答案