吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 195|回复: 7
收起左侧

[经验求助] 求助C#编程问题,为什么载入内嵌RTF富文本提示非法字符

[复制链接]
烟99 发表于 2024-3-5 21:19
300吾爱币
这是一个窗口程序,窗口加载完毕后,将如下图所示的“MyRTF.rtf”内嵌到resource.resx中,并按照文件内的原始格式加载到富文本控件 richTextBox1中

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.LoadFile(Properties.Resources.MyRTF, RichTextBoxStreamType.RichText);
        }
    }
}


我已经将MyRTF.rtf嵌入到resx里了,编译时候也没有报错。但启动时候提示System.ArgumentException:“路径中具有非法字符。”
不知道这是为什么




如果这样做不对的话,那么C#如何将内嵌的RTF文本加载到富文本框内,第一次接触C#,希望大佬指点
在此谢过

最佳答案

查看完整内容

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Light紫星 发表于 2024-3-5 21:19
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.LoadFile(new MemoryStream(Properties.Resources.MyRTF), RichTextBoxStreamType.RichText);
        }
    }
}

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
烟99 + 5 + 1 感谢,已经成功!

查看全部评分

Light紫星 发表于 2024-3-5 21:25
loadfile应该是从路径加载文件,你找找有没有直接加载bytes的

点评

百度搜的加载内嵌文件都是这样写的  详情 回复 发表于 2024-3-5 21:29
nasidden 发表于 2024-3-5 21:27
1 建议文件路径没有中文字符
2 注意全半角
3复制粘贴后带有特殊字符,(好多网站复制带有特殊字符)

点评

绝对没有中文字符  详情 回复 发表于 2024-3-5 21:29
Light紫星 发表于 2024-3-5 21:29
richTextBox1.LoadFile(new MemoryStream(bytes), RichTextBoxStreamType.RichText);
 楼主| 烟99 发表于 2024-3-5 21:29
Light紫星 发表于 2024-3-5 21:25
loadfile应该是从路径加载文件,你找找有没有直接加载bytes的

百度搜的加载内嵌文件都是这样写的
 楼主| 烟99 发表于 2024-3-5 21:29
nasidden 发表于 2024-3-5 21:27
1 建议文件路径没有中文字符
2 注意全半角
3复制粘贴后带有特殊字符,(好多网站复制带有特殊字符)

绝对没有中文字符
爱飞的猫 发表于 2024-3-5 21:43

LoadFile 方法有三个原型:

你要的应该是第二个,从内存流加载,而不是另外两个从文件路径加载。


试试把资源改成二进制,然后读取资源的时候把数据改成内存数据流:

        private void Form1_Load(object sender, EventArgs e)
        {
            using(var stream = new MemoryStream())
            {
                stream.Write(Properties.Resources.test, 0, Properties.Resources.test.Length);
                stream.Position = 0;

                this.richTextBox1.LoadFile(stream, RichTextBoxStreamType.RichText);
            }
        }

Properties.Resources.test 中的 test 是我的资源名)

编译后执行的效果如下:

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-22 05:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表