问题描述
我想从文件中找到非ASCII字符,并在文本文件中用类似的字母替换它们。我有2个文本文件,看起来像LS_PRSADT _ * .txt,我已经部署了一个带有NON Ascii代码和类似外观字符的表。现在我调用SP并从文件中获取输入(NON-ASCII)并用类似的字母替换它们。我有以下代码。我从一个目录中读取2个文本文件,我还需要将这两个文本文件保存在同一个位置,并且名称相同。不知何故,它无法正常工作。请帮助
I want to find the NON ASCII characters from the files and replace them by similar looking Alphabets in text file. I have 2 text files which are looking like LS_PRSADT_*.txt and I have deployed a table with NON Ascii codes and similar looking characters . Now I am calling the SP and take the input(NON-ASCII) from file and replace them by similar looking alphabets. I have the following code . Where I am reading 2 text files from one one directory and I also need to save those two text files in the same location and also with the same names. Somehow It is not working properly. Please help
static string CS = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString.ToString();
static void Main(string[] args)
{
string date = DateTime.Today.ToString("yyyyMMdd");
string date2=DateTime.Today.ToString("YYYYMMMDD");
string dirPath =
string quarintine =
string filepattern1 = "LS_PRSADT_*.txt";
string backupfile = Path.Combine(quarintine, date);
string movefiles = Path.Combine(quarintine, date2);
string[] filesDT = Directory.GetFiles(dirPath, filepattern1);
string s1 = null;
string s2 = null;
Directory.CreateDirectory(backupfile);
filesDT.ToList().ForEach(k => File.Copy(k, Path.Combine(backupfile, Path.GetFileName(k))));
for (int i = 0; i < filesDT.Length; i++)
{
string file = filesDT[i];
FileInfo fileinfo = new FileInfo(file);
string content = File.ReadAllText(fileinfo.FullName);
foreach (char c in content)
{
if (c > 127)
{
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand sqlcom = new SqlCommand("sp_ReplaceNonAsciiCharset", con);
sqlcom.CommandType = CommandType.StoredProcedure;
SqlParameter sqlparam = new SqlParameter();
sqlparam.ParameterName = "@non_ascii_char";
sqlparam.Direction = ParameterDirection.Input;
sqlparam.SqlDbType = SqlDbType.NChar;
sqlparam.Size = 2;
sqlparam.Value = c;
sqlcom.Parameters.Add(sqlparam);
object o = sqlcom.ExecuteScalar();
int j = content.IndexOf(c);
s1 = content.Remove(j, 1);
s2 = s1.Insert(i, o.ToString());
content = s2;
}
}
}
filesDT[i] = content;
推荐答案
这篇关于用类似的字母替换NON-ASCII字符,用空格替换其他字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!