使用memorystream复制 [英] Copy with memorystream

查看:180
本文介绍了使用memorystream复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

问题:我有多个文件.这些文件通过使用列表框显示.当从列表框中选择任何一个文件时,应将其复制到另一个文件中,而我不希望文件的全部内容,我只希望将文件的一部分复制到另一个文件中.我认为这可以通过内存流来完成.

我写了这么多的代码

string sourceFile = listBox1.SelectedItem.ToString();
            
 string destinationFile = @"D:\Sowjanya\Directx files\Hero.X";
            try
            {
                //causes exception if already exists.
                File.Delete(destinationFile);
                string str1="animationset";
                if(sourceFile.Contains(str1))
                File.Copy(sourceFile, destinationFile);
                MessageBox.Show("Copy completed.");
            }

            catch (Exception e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.Read();


它被复制了文件的全部内容...但是我只想要一些部分文本
即如果文件包含animationset方法,则必须将整个方法复制到另一个文件中.所选文件必须具有这样的animationset方法

animatinset
{
.......
}


这就是我想要的

请帮助我.

解决方案

sowjanyagadela写道:

它被复制了全部内容文件....但是我只希望部分文本



好吧,您DID调用File.Copy.不需要内存流.您需要File.ReadAlllines或File.ReadAllBytes来读取内存中的整个文件,然后需要解析文件以查找要复制的位,然后可以使用类似的重载来写入新文件片段.


Hi All,

Problem: I have a multiple files. These files displayed by using listbox. When select any one of the file from listbox it should be copied into another file and I don''t want the full content of file I want only some part of a file copied into another file. I think this can be done through the memory stream.

I wrote this much of code

string sourceFile = listBox1.SelectedItem.ToString();
            
 string destinationFile = @"D:\Sowjanya\Directx files\Hero.X";
            try
            {
                //causes exception if already exists.
                File.Delete(destinationFile);
                string str1="animationset";
                if(sourceFile.Contains(str1))
                File.Copy(sourceFile, destinationFile);
                MessageBox.Show("Copy completed.");
            }

            catch (Exception e1)
            {
                Console.WriteLine(e1.Message);
            }
            Console.Read();


It was copied whole content of file ... but I want some part text only
i.e if the file contains the animationset method that whole method must be copied into antoher file. That selected file is must be having an animationset method like this

animatinset
{
.......
}


This is what I want

Please help me.

解决方案

sowjanyagadela wrote:

It was copied whole content of file.... but i want some part text only



Well, you DID call File.Copy. Yo don''t need a memory stream. You need File.ReadAlllines or File.ReadAllBytes to read the whole file in memory, then you need to parse the file to find the bits you want to copy, then you can use a similar overload to write the new file fragment.


这篇关于使用memorystream复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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