将richtextbox保存到文件C# [英] save richtextbox to file C#

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

问题描述

我无法从Richtextbox保存到.txt文件

I am having trouble saving from richtextbox to .txt file

下面是代码:

if (richTextBox1.Text != String.Empty)
            {
                string dir = @"c:\\logs\\" + DateTime.Today.ToString("dd_MMM_yy");
                string path = @"c:\\logs\\" + DateTime.Today.ToString("dd_MMM_yy") + "\\" + DateTime.Now.ToString("HH.mm.ss") + ".txt";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                if (!File.Exists(path))
                {
                    File.Create(path);
                    richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
                }

            }
            else
                MessageBox.Show("ERROR");

我要去哪里错了?它说它无法访问文件,因为它正在被另一个进程使用...欢迎任何帮助

where I am going wrong ?! It says it cannot access the file because it is being used by another process... Any help would be welcome

谢谢,丹尼斯科

推荐答案

您可以避免创建文件行,因为 SaveFile 会为您创建文件.

you can avoid create file line because SaveFile will create file for you.

File.Create 将返回文件的打开流,您需要先关闭它,然后才能再次访问.如果您仍然需要使用创建文件,请执行以下操作

File.Create will return open stream for the file, you need to close it before access again. Do as below If you need to use create file anyway

using(File.Create(path));
richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);

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

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