C#中文件操作IO

StreamReader

StreamReader sr = new StreamReader("qiufeng.txt", Encoding.GetEncoding("gb2312"));
            Console.WriteLine(sr.ReadLine());
            Console.Wri
teLine(sr.ReadLine());

使用ReadLine()读取,每次读一行,并且每次读取的时候,从上次读取的下一行开始,并且,当文档读完的时候,返回空

string str2 = string.Empty;
            while ((str2 = sr.ReadLine()) != null)
            {
                Console.WriteLine(str2);
            }

读取文档的每一行。他和string str3 = sr.ReadToEnd();

是等价的。

WriteAllLines

string[] str = { 
                            "山中相送罢,",
                            "日暮掩柴扉。",
                            "春草明年绿,",
                            "王孙归不归。"     
                           };
            File.WriteAllLines("qiufeng.txt", str);

会写入qiufeng.txt,并且如果原来文档中有数据会清空再增加

File.WriteAllText(@"D:\2.txt",sb.ToString());

sb是一个StringBuilder,

 File.AppendAllText("qiufeng.txt", str1)是追加数据
 FileStream
复制视频
 FileStream fs = new FileStream(@"d:\01复习1.avi", FileMode.Open, FileAccess.Read);
            FileStream fs2 = new FileStream(@"d:\01复习2.avi", FileMode.Create, FileAccess.Write);
            byte[] bs = new byte[1024*1024*10];
            using (fs)
            {
                using (fs2)
                {
                    int count;
                    do
                    {
                        count = fs.Read(bs, 0, bs.Length);
                        fs2.Write(bs, 0, count);
                    } while (count != 0);
                }
            }
先将数据用byte[]数组接受,注意我们这里是视频文件,如果数组设置过小,会造成频繁读取硬盘,我们这里设置为10M;
文件的加密解密:
我们可以在字节中插入密钥,如加1
using(FileStream fs1=new FileStream(@"d:\girl.mp3",FileMode.Open,FileAccess.Read))
            {
                using (FileStream fs2 = new FileStream(@"d:\newgirl.mp3", FileMode.Create, FileAccess.Write))
                {
                    //MP3设置一M就可以了
                    byte[] bs = new byte[1024 * 1024];
                    int count = 0;
                    while ((count = fs1.Read(bs, 0, bs.Length)) > 0)
                    {
                        //有了一个字节数组
                        //如果将每个字节原封不动的写入新文件中,则表示copy
                        //如果要加密,需要一个密钥,咱就设为1,在每一个字节上加上一个数字1
                        for (int i = 0; i < bs.Length; i++)
                        {
                            bs[i]++;
                        }
                        fs2.Write(bs, 0, count);
                    }
                }
            }
解密则是减去一:
using (FileStream fs1 = new FileStream(@"d:\newgirl.mp3", FileMode.Open, FileAccess.Read))
            {
                using (FileStream fs2 = new FileStream(@"d:\DeCodegirl.mp3", FileMode.Create, FileAccess.Write))
                {
                    //MP3设置一M就可以了
                    byte[] bs = new byte[1024 * 1024];
                    int count = 0;
                    while ((count = fs1.Read(bs, 0, bs.Length)) > 0)
                    {
                        //有了一个字节数组
                        //如果将每个字节原封不动的写入新文件中,则表示copy
                        //如果要加密,需要一个密钥,咱就设为1,在每一个字节上加上一个数字1
                        for (int i = 0; i < bs.Length; i++)
                        {
                            bs[i]--;
                        }
                        fs2.Write(bs, 0, count);
                    }
                }
            }

转载于:https://www.cnblogs.com/automation/archive/2013/01/22/2871488.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#文件操作的相对路径是指相对于当前工作目录的路径。当前工作目录是指程序运行时所在的目录。在C#,可以使用System.IO.Path类的一些方法来处理文件路径操作。具体来说,可以使用Path类的Combine方法来拼接路径,使用GetDirectoryName方法来获取路径的目录部分,使用GetFileName方法来获取路径的文件名部分,使用GetExtension方法来获取路径的扩展名部分等等。 举个例子,假设当前工作目录是"D:\MyProject",而需要操作文件在该目录下的"Data"文件的"sample.txt"文件。那么可以使用Path类的Combine方法来拼接路径,代码如下: string filePath = Path.Combine("Data", "sample.txt"); 这样就得到了相对路径"Data\sample.txt"。在进行文件操作时,可以使用这个相对路径来指定要操作文件。 需要注意的是,相对路径是相对于当前工作目录的,所以在不同的环境下,当前工作目录可能会有所不同。为了确保代码的可移植性,可以使用Path类的GetFullPath方法来获取完整的路径,代码如下: string fullPath = Path.GetFullPath(filePath); 这样就可以获取到"D:\MyProject\Data\sample.txt"这个完整的路径,无论当前工作目录是什么。 综上所述,C#文件操作的相对路径是相对于当前工作目录的路径。可以使用Path类的一些方法来处理文件路径操作,如拼接路径、获取目录部分、获取文件名部分等。在进行文件操作时,可以使用相对路径来指定要操作文件。为了确保代码的可移植性,可以使用GetFullPath方法来获取完整的路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [浅析C#文件路径的操作](https://download.csdn.net/download/weixin_38715097/12790167)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [C#操作文件系统绝对路径和相对路径](https://download.csdn.net/download/zhwcd/10670331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C#winform使用相对路径读取文件的方法](https://download.csdn.net/download/weixin_38660069/13991652)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值