[英]Data from newly created .text file is not readable to third party application
我已經開發了一個Windows應用程序,它將從.jrn文件(在ATM機中)讀取更新的數據,並將文本復制到臨時文本文件“ tempfile.txt”中。
還有另一個名為“ POS Text Sender”的第三方應用程序,它讀取“ tempfile.txt”並在CCTV攝像機中顯示其內容。
問題是,如果我直接在tempfile中鍵入內容,則POS應用程序將讀取該內容,但是如果我的應用程序將文本寫入“ tempfile”,則可以看到與tempfile中.jrn文件相同的內容,但不是數據從新生成的文件復制到tempfile時會反映在POS應用程序中。如果從新生成的文件將第一個數據復制到tempfile后重新啟動POS文本發送器,則POS文本發送器將顯示內容,直到新創建的文件寫入內容為止到臨時文件
我的應用程序代碼是使用StreamReader讀取.jrn文件並將其分配給字符串變量,然后使用StreamWriter將其寫入臨時文件。 手動在文件上鍵入文本與.NET StreamWriter將文本寫入文件有什么區別?
碼:
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)
{
}
}
您可以檢查以下內容:
編輯:
另外,在發布的代碼中,您將tempFile數據與當前數據進行比較,如果tempFile數據比當前數據新,則將附加temp文件,我認為反之亦然。
主要變化:
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:您可以將復制倉庫更改為此,並看到問題仍然存在:
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)
{
}
}
這很可能是CR + LF問題。 POS期望文件的行尾以CR + LF(回車(0x0D)+新行(0x0A))組合。
filenew.WriteLine(originalcontent)
僅附加換行符。 我認為,鍵入時,您的編輯器必須為所有行尾創建CR + LF組合。
我建議您嘗試filenew.Write( originalcontent + "\\r\\n");
一個區別是您的應用程序不直接寫入tempfile.txt,而是直接寫入另一個文件,然后將該文件復制到tempfile.txt。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.