我开发了一个Windows应用程序,它将从.jrn文件(在ATM机中)读取更新的数据,并将文本复制到临时文本文件“tempfile.txt”。
还有另一个名为“POS Text Sender”的第三方应用程序,它读取“tempfile.txt”并在CCTV摄像机中显示它的内容。
问题是如果我在tempfile中直接键入内容,POS应用程序将读取它,但如果我的应用程序将文本写入“tempfile”,我可以看到与tempfile中的.jrn文件相同的内容,但是当数据从新生成的文件复制到tempfile时,它不会反映在POS应用程序中。如果在从新生成的文件复制到tempfile的第一个数据后重新启动POS文本发送器,POS文本发送器将显示新创建的内容直到内容文件被写入tempfile
我的应用程序代码是使用StreamReader读取.jrn文件并将其分配给字符串变量,然后使用StreamWriter将其写入tempfile。手动在文件上键入文本和.NET StreamWriter将文本写入文件有什么区别?
CODE:
DateTime LastChecked = DateTime.Now;
try
{
string[] files = System.IO.Directory.GetFiles(@"C:\Test", "*.jrn", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
if (!fileList.Contains(file))
{
currentfilename = file;
fileList.Add(file);
copywarehouse(file);
//do_some_processing();
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(file))
{
currentcontent=sr.ReadToEnd();
// Read and display lines from the file until the end of
//// the file is reached.
//while ((currentcontent = sr.ReadLine()) != null)
//{
//}
sr.Close();
//sr.Dispose();
}
}
catch (Exception)
{
// Let the user know what went wrong.
}
}
}
//checking
try
{
using (StreamReader sr = new StreamReader(currentfilename))
{
string currentfilecontent = sr.ReadToEnd();
sr.Close();
//sr.Dispose();
if (currentfilecontent!=currentcontent)
{
if (currentfilecontent.Contains(currentcontent))
{
string originalcontent = currentfilecontent.Substring(currentcontent.Length);
System.IO.StreamWriter filenew = new System.IO.StreamWriter(@"C:\Test\tempfile.txt");
filenew.WriteLine(originalcontent);
filenew.Close();
currentcontent = currentfilecontent;
}
}
}
}
catch (Exception)
{
// Let the user know what went wrong.
}
copywarehouse
方法:
private void copywarehouse(string filename)
{
string sourcePath = @"C:\Test";
string targetPath = @"C:\Test";
try
{
string sourceFile = System.IO.Path.Combine(sourcePath, filename);
string destFile = System.IO.Path.Combine(targetPath, "tempfile.txt");
System.IO.File.Copy(sourceFile, destFile, true);
}
catch (Exception)
{
}
}
答案 0 :(得分:3)
您可以查看以下内容:
修改强>
此外,在您发布的代码中,您正在将tempFile数据与当前数据进行比较,如果tempFile数据比当前数据更新,则附加临时文件,我认为反之亦然。< /强>
主要变化:
using (StreamWriter filenew = new StreamWriter(fileDetail.TempFileName, true, Encoding.ASCII))
{
filenew.WriteLine(newContent);
}
要知道正确的编码,只需创建一个新的tempFile,在编辑器中编写并保存。在记事本中打开文件并执行“另存为”。这将在底部显示当前编码。在.NET代码中设置该编码。
如果这不起作用,请尝试(按照shr的建议):
using (StreamWriter filenew = new StreamWriter(fileDetail.TempFileName, true, Encoding.ASCII))
{
filenew.Write(newContent + "\r\n");
}
长版本:(可能与您的代码略有不同):
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime LastChecked = DateTime.Now;
IDictionary<string, FileDetails> fileDetails = new Dictionary<string, FileDetails>(StringComparer.OrdinalIgnoreCase);
IList<string> tempFileList = new List<string>();
try
{
string[] files = System.IO.Directory.GetFiles(@"C:\Test", "*.jrn", System.IO.SearchOption.AllDirectories);
foreach (string file in files)
{
string currentfilename = file;
string currentcontent = string.Empty;
if (!fileDetails.Keys.Contains(file))
{
fileDetails[file] = new FileDetails(copywarehouse(file));
//do_some_processing();
}
try
{
using (StreamReader sr = new StreamReader(file))
{
currentcontent = sr.ReadToEnd();
}
}
catch (Exception)
{
// Let the user know what went wrong.
}
fileDetails[file].AddContent(currentcontent);
}
//TODO: Check using the file modified time. Avoids unnecessary reading of file.
foreach (var fileDetail in fileDetails.Values)
{
//checking
try
{
string tempFileContent = string.Empty;
string currentcontent = fileDetail.GetContent();
using (StreamReader sr = new StreamReader(fileDetail.TempFileName))
{
tempFileContent = sr.ReadToEnd();
sr.Close();
}
if (!(0 == string.Compare(tempFileContent, currentcontent)))
{
if (currentcontent.Contains(tempFileContent))
{
string newContent = tempFileContent.Substring(currentcontent.Length);
using (StreamWriter filenew = new StreamWriter(fileDetail.TempFileName, true, Encoding.ASCII))
{
filenew.WriteLine(newContent);
}
}
}
}
catch (Exception)
{
// Let the user know what went wrong.
}
}
}
catch (Exception)
{
}
}
private static string copywarehouse(string filename)
{
string sourcePath = @"C:\Test";
string targetPath = @"C:\Test";
string sourceFile = System.IO.Path.Combine(sourcePath, filename);
string destFile = System.IO.Path.Combine(targetPath, filename+ "tempfile.txt");
try
{
System.IO.File.Copy(sourceFile, destFile, true);
}
catch (Exception)
{
}
return destFile;
}
internal class FileDetails
{
public string TempFileName { get; private set; }
private StringBuilder _content;
public FileDetails(string tempFileName)
{
TempFileName = tempFileName;
_content = new StringBuilder();
}
public void AddContent(string content)
{
_content.Append(content);
}
public string GetContent()
{
return _content.ToString();
}
}
}
}
编辑2: 你能不能将copywarehouse改成这个并且看到问题仍然存在:
private void copywarehouse(string filename)
{
const string sourcePath = @"C:\Test";
const string targetPath = @"C:\Test";
try
{
string sourceFile = Path.Combine(sourcePath, filename);
string destFile = Path.Combine(targetPath, "tempfile.txt");
string currentcontent;
using (var sr = new StreamReader(sourceFile))
{
currentcontent = sr.ReadToEnd();
}
using (var wr = new StreamWriter(destFile, false, Encoding.ASCII))
{
wr.WriteLine(currentcontent);
}
}
catch (Exception)
{
}
}
答案 1 :(得分:2)
这很可能是CR + LF问题。 POS期望文件具有CR + LF(回车(0x0D)+新行(0x0A))组合的行结尾。
filenew.WriteLine(originalcontent)
仅追加新的换行符。当你键入时,我认为,编辑器必须为所有行结尾创建CR + LF组合。
我建议你试试filenew.Write( originalcontent + "\r\n");
答案 2 :(得分:1)
一个区别是您的应用程序不直接写入tempfile.txt而是写入另一个文件,然后将该文件复制到tempfile.txt。