C#:在再次保存之前无法读取rtf [英] C#: can't read rtf until saved again

查看:79
本文介绍了C#:在再次保存之前无法读取rtf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我有一个复杂的c#程序,它从多个RTF文件中获取数据,将它们合并到一个TXT文件中,我可以搜索来自串口的特定数字。



但是我有一个非常有趣的问题。



我每天得到5-10个RTF文件,但我的浏览器无法访问它们。 虽然如果我打开一次,编辑没有,只需再次保存(即使是同名)。我可以访问它们并且没有问题地阅读它们。

我甚至尝试过其他编程语言(VB)但是也没用,所以文件有问题。

如果我在Total Commander中比较它们的结果:(再次:相同的文件,只是在同一个名字上打开并保存,没有被编辑



不工作的文件:

 {\rtf1\ansi\deff0\deflang1024 
{\ t​​tonttbl
{\f1\fswiss Arial CE;}
{\f2 \fswiss Arial CE;}
{\ f3 \fswiss Arial CE;}
{\f4 \fswiss Arial CE;}}
{\ colortbl;
\red0\green0\blue0;
\ red0 \ green0 \ blue255;
\\ \\ red0\green255\blue255;
\\\green255 \ blue0;
\red255 \ green0 \ blue255;
\red255 \ green0 \ blue05 ;
\red255 \green255 \ blue05;
\ red255 \ green255 \ blue255;
\\\ green0 \ blue127;
\\\green127 \ blue127;
\\\green127 \ blue05;
\red127 \ green0 \ blue127;
\red127\green0\blue0;
\red127 \green127 \ blue12;
\red127 \green127 \ blue127;
\red192\green192 \ blue192;}
{\ buddlesheet
{\\\\\\\\\\\\\\\\\\\\\\\\ $ b \paperw11908\paperh16833\margl567\margr567\margt849\margb849
\gutter0\ftnbj\sectd\linex0\endnhere\pard\plain\fs20\ lang1033





工作开始的文件:



 {\rtf1\adeflang1025\ansi\ansicpg1250\uc1\adeff0\deff0\stshfdbch31505\stshfloch31506\stshfhich31506\stshfbi31507\deflang1038\deflangfe1038\themelang1038\themelangfe0 \themelangcs0 {{\fonttbl \f0\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;} {\f1\fbidi \fswiss\fcharset238 \ fprq2 {\ * \ panose 020b0604020202020204} Arial;} 
{\f34\fbidi \\\ tfrman \ fcha rset1\fprq2 {\ * \panose 00000000000000000000}坎布里亚数学;} {\flomajor\f31500\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;}
{\fdbmajor\f31501\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;} {\fhimajor\f31502\fbidi \froman \fcharset238\fprq2 {\ * \panose 02040503050406030204}坎布里亚;}
{\fbimajor\f31503\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;} {\flominor\f31504\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;}
{\fdbminor\f31505\\ \\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;} {\fhiminor\f31506\fbidi \fswiss\fcharset238\fprq2 {\ * \\ \\panose 020f0502020204030204}宋体;}
{\fbiminor\f31507\fbidi \froman\fcharset238\fprq2 {\ * \panose 02020603050405020304}宋体;} {\f42\ fbidi \froman\fcharset0\fprq2 Times New Roman;} {\f41\fbidi \froman\fcharset204\fprq2 Times New Roman Cyr;}
{\f43 \ fbidi \ froman\fcharset161\fprq2宋体希腊;} {\f44\fbidi \froman\fcharset162\fprq2宋体图尔;} {\f45\fbidi \froman\fcharset177\ fprq2 Times New Roman(希伯来语);} {\f46 \ fbidi \froman \fcharset178 \fprq2 Times New Roman(阿拉伯语);}
{\f47 \ fbidi \froman\fcharset186 \ fprq2 Times New Roman Baltic;} {\\\\\\\\\\\\\\\\\\\\\\\\ ;} {\f51\fbidi \fswiss \fchars et204\fprq2宋体CYR;}
{\f53\fbidi \fswiss\fcharset161\fprq2希腊宋体;} {\f54\fbidi \fswiss\fcharset162\fprq2宋体图尔;} {\f55\fbidi \fswiss\fcharset177\fprq2 Arial字体(希伯来语);} {\f56\fbidi \fswiss\fcharset178\fprq2 Arial字体(阿拉伯);}
{\f57\fbidi \fswiss\fcharset186\fprq2 Arial字体巴尔;} {\f58\fbidi \fswiss\fcharset163\fprq2 Arial字体(越南);} {\flomajor\f31510\\ \\ fbidi \froman\fcharset0\fprq2 Times New Roman;} {\\\\\\\\\\\\\\\\\\\\\\\\\遇到 ANYTHING 之前或者 ANY 想出来解决这个问题吗?



我们非常感谢您的帮助!

最好的问候,

亚当



我的尝试:



一切

尝试以各种方式访问​​它们,没有一个似乎工作,直到我手动保存它们

解决方案

不工作文件指定索引为零的字体作为默认字体( \deff0 )但是字体表中没有匹配的条目(索引1到4只有条目)。



所以没有明确的任何文本无法呈现对字体的引用。


好的。正式解决,你们都会笑我。



而不是:

 FolderBrowserDialog fd = < span class =code-keyword> new  FolderBrowserDialog(); 
fd.SelectedPath = @ C:\BEVONT \;
fd.ShowDialog();
string path = fd.SelectedPath;

foreach string file in Directory.GetFiles(path))
{
string contents = File.ReadAllText(file);
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Rtf = contents + Environment.NewLine;
string plainText = richTextBox1.Text;
File.AppendAllText( @ N:\Kártyaellenőr\ Konvert\Convert _ + DateTime.Now.ToString( yyyyMMdd)+ .txt,内容);
}





我刚刚删除了一些行,现在它正在运行。以前没有尝试过这个让我感到羞耻:

 FolderBrowserDialog fd =  new  FolderBrowserDialog(); 
fd.SelectedPath = @ C:\BEVONT \;
fd.ShowDialog();
string path = fd.SelectedPath;

foreach string file in Directory.GetFiles(path))
{
string contents = File.ReadAllText(file);
File.AppendAllText( @ N:\Kártyaellenőr\ Konvert\Convert _ + DateTime.Now.ToString( yyyyMMdd)+ .txt,内容);
}


Hello! I have a complex c# program that gets data from multipe RTF files, merge them into one TXT file and I can search specific digits in it which are coming from serial port.

I have one very interesting problem, though.

I get 5-10 RTF files every day, but my browser can't access them. ALTHOUGH If I open them once, EDIT NOTHING, just save them again (even if on the same name). I can access them and read them with no problem.
I even tried other programming language (VB) but that didn't work either, so the files has some problem.
If i compare them in Total Commander that's the result: (AGAIN: SAME FILES, JUST OPENED AND SAVED AGAIN ON TH SAME NAME, NOTHING EDITED

THE FILE WHICH IS NOT WORKING STARTS WITH:

{\rtf1\ansi\deff0\deflang1024
{\fonttbl
{\f1\fswiss Arial CE;}
{\f2\fswiss Arial CE;}
{\f3\fswiss Arial CE;}
{\f4\fswiss Arial CE;}}
{\colortbl;
\red0\green0\blue0;
\red0\green0\blue255;
\red0\green255\blue255;
\red0\green255\blue0;
\red255\green0\blue255;
\red255\green0\blue0;
\red255\green255\blue0;
\red255\green255\blue255;
\red0\green0\blue127;
\red0\green127\blue127;
\red0\green127\blue0;
\red127\green0\blue127;
\red127\green0\blue0;
\red127\green127\blue0;
\red127\green127\blue127;
\red192\green192\blue192;}
{\stylesheet
{\fs20\lang1033\snext0 Normal;}}
\viewkind1
\paperw11908\paperh16833\margl567\margr567\margt849\margb849
\gutter0\ftnbj\sectd\linex0\endnhere\pard\plain\fs20\lang1033



THE FILE THAT IS WORKING STARTS WITH:

{\rtf1\adeflang1025\ansi\ansicpg1250\uc1\adeff0\deff0\stshfdbch31505\stshfloch31506\stshfhich31506\stshfbi31507\deflang1038\deflangfe1038\themelang1038\themelangfe0\themelangcs0{\fonttbl{\f0\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f1\fbidi \fswiss\fcharset238\fprq2{\*\panose 020b0604020202020204}Arial;}
{\f34\fbidi \froman\fcharset1\fprq2{\*\panose 00000000000000000000}Cambria Math;}{\flomajor\f31500\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}
{\fdbmajor\f31501\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\fhimajor\f31502\fbidi \froman\fcharset238\fprq2{\*\panose 02040503050406030204}Cambria;}
{\fbimajor\f31503\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\flominor\f31504\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}
{\fdbminor\f31505\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\fhiminor\f31506\fbidi \fswiss\fcharset238\fprq2{\*\panose 020f0502020204030204}Calibri;}
{\fbiminor\f31507\fbidi \froman\fcharset238\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f42\fbidi \froman\fcharset0\fprq2 Times New Roman;}{\f41\fbidi \froman\fcharset204\fprq2 Times New Roman Cyr;}
{\f43\fbidi \froman\fcharset161\fprq2 Times New Roman Greek;}{\f44\fbidi \froman\fcharset162\fprq2 Times New Roman Tur;}{\f45\fbidi \froman\fcharset177\fprq2 Times New Roman (Hebrew);}{\f46\fbidi \froman\fcharset178\fprq2 Times New Roman (Arabic);}
{\f47\fbidi \froman\fcharset186\fprq2 Times New Roman Baltic;}{\f48\fbidi \froman\fcharset163\fprq2 Times New Roman (Vietnamese);}{\f52\fbidi \fswiss\fcharset0\fprq2 Arial;}{\f51\fbidi \fswiss\fcharset204\fprq2 Arial Cyr;}
{\f53\fbidi \fswiss\fcharset161\fprq2 Arial Greek;}{\f54\fbidi \fswiss\fcharset162\fprq2 Arial Tur;}{\f55\fbidi \fswiss\fcharset177\fprq2 Arial (Hebrew);}{\f56\fbidi \fswiss\fcharset178\fprq2 Arial (Arabic);}
{\f57\fbidi \fswiss\fcharset186\fprq2 Arial Baltic;}{\f58\fbidi \fswiss\fcharset163\fprq2 Arial (Vietnamese);}{\flomajor\f31510\fbidi \froman\fcharset0\fprq2 Times New Roman;}{\flomajor\f31509\fbidi



anyone encountered ANYTHING like that before or has ANY idea to work it out?

Every help is much appreciated!
Best Regards,
Adam

What I have tried:

Everything
tried to access them various ways, none of them seems to work until i save them manually

解决方案

The not working file specifies the font with index zero as default font (\deff0) but there is no matching entry in the font table (there are only entries for indexes 1 to 4).

So any text without an explicit reference to a font can't be rendered.


Okay. Officially solved, you will all laugh on me.

Instead of this:

FolderBrowserDialog fd = new FolderBrowserDialog();
fd.SelectedPath = @"C:\BEVONT\";
fd.ShowDialog();
string path = fd.SelectedPath;

foreach (string file in Directory.GetFiles(path))
{
string contents = File.ReadAllText(file);
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Rtf = contents + Environment.NewLine;
string plainText = richTextBox1.Text;
File.AppendAllText(@"N:\Kártyaellenőr\Konvert\Convert_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", contents);
}



I just deleted some lines, and it's working now. Shame on me for not trying this before:

FolderBrowserDialog fd = new FolderBrowserDialog();
fd.SelectedPath = @"C:\BEVONT\";
fd.ShowDialog();
string path = fd.SelectedPath;

foreach (string file in Directory.GetFiles(path))
{
string contents = File.ReadAllText(file);
File.AppendAllText(@"N:\Kártyaellenőr\Konvert\Convert_" + DateTime.Now.ToString("yyyyMMdd") + ".txt", contents);
}


这篇关于C#:在再次保存之前无法读取rtf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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