Win7 CSV编辑程序在Win Server 2008下无法运行 [英] Win7 CSV Edit programm not working under Win Server 2008
问题描述
大家好,我有点问题。
i编写了一个C#app来编辑一些.csv文件(过滤器换行符和结束行)以进行导入更容易进入另一个系统。
该程序在5台机器上运行良好,所有win7都没有任何问题,并在30秒内完成任务,但一旦我尝试在win 2008 R2服务器上运行它就会冻结。这是代码:
通过txt文件读取文件路径,以便在源文件夹或目标文件夹更改时更灵活。以及需要在csv末尾删除的行的txt文件。
主表单(使用计时器)
private void timer1_Tick( object sender,EventArgs e)
{
try
{
// todo Read Configfile
string sourcePath = Application.StartupPath + \\SourceFolder.txt;
string targetPath = Application.StartupPath + \\TargetFolder.txt跨度>;
srSource = new StreamReader(sourcePath);
srTarget = new StreamReader(targetPath);
string target = ;
while (!srTarget.EndOfStream)
{
target = srTarget.ReadLine();
}
// testing
writeError(System.DateTime。 Now.ToString()+ - >开始写入目标: + target);
// 测试没问题。文件已写入
而(!srSource.EndOfStream)
{
string line = srSource.ReadLine();
string [] filePaths = Directory.GetFiles(line, * CSV跨度>);
foreach (字符串文件 in filePaths)
{
try
{
FileInfo fi = new FileInfo(文件);
ChristmasCards ccrd = new ChristmasCards(Application.StartupPath + \\ConfigEndings.txt跨度>); // 结束行的条件(每行是需要删除的行)
if (ccrd.OK)
{
DirectoryInfo dir = new DirectoryInfo(target );
if (!dir.Exists)
{
dir.Create();
}
FileInfo destfi = new FileInfo(target + \\ + fi.Name);
writeError(System.DateTime.Now.ToString()+ - > Readinfo Target + target + \\ + fi.Name); // 写好!
if (destfi.Exists)
{
destfi.Delete();
}
// 执行流程
string result = ccrd.ReadCSV(file,target + \\ + fi.Name);
// 检查一切是否保存好
if (result == OK)
writeError(System.DateTime.Now.ToString()+ - > + \\\\ nnFile保存为: + target + \\ + fi.Name);
else
writeError(System.DateTime.Now.ToString()+ - > + \\\\ nnError文件保存: +结果);
}
else
{
writeError(System.DateTime.Now.ToString()+ - > + \\\\ nnError Configfile不可读);
srSource.Close();
}
}
catch (Exception ex)
{
writeError(System.DateTime.Now。 ToString()+ - > + \\\\ nnError: + ex.Message);
srSource.Close();
}
}
}
srSource.Close();
srTarget.Close();
}
catch (Exception ex)
{
writeError(System.DateTime.Now.ToString()+ < span class =code-string> - > + \\\\ nnError: + ex.Message);
}
最后
{
此。关闭();
Application.Exit();
}
}
private void writeError( string message)
{
StreamWriter errorwriter = new StreamWriter(Application.StartupPath + \\Errors.txt, true , System.Text.Encoding.Default);
errorwriter.WriteLine(System.DateTime.Now.ToString()+ - > +消息);
errorwriter.Close();
}
圣诞贺卡课程功能
StreamReader条件阅读器;
StreamReader csv;
StreamWriter csvwrite;
bool ok = false ;
ArrayList cond = new ArrayList();
#region constructor
public ChristmasCards( string configfile)
{
try
{
conditionsreader = new StreamReader(configfile);
while (!conditionsreader.EndOfStream)
{
cond.Add(conditionsreader.ReadLine());
}
conditionsreader.Close();
ok = true ;
}
catch (例外情况)
{
ok = false 跨度>;
}
}
public string ReadCSV( string filename, string destfilename)
{
try
{
bool nextline = false ;
bool write = true ;
// 打开文件data.csv,这是一个带头文件的CSV文件
csv = new StreamReader(filename,System.Text.Encoding.Default);
csvwrite = new StreamWriter(destfilename, true ,System.Text.Encoding.Default);
while (!csv.EndOfStream)
{
// 删除换行符
string line = csv.ReadLine();
if (!line.EndsWith( \))
{
nextline = true ;
while (nextline)
{
line + = csv.ReadLine();
if (line.EndsWith ( \))
nextline = 假跨度>;
}
}
// 过滤结束行
foreach ( string s in cond)
{
if (line.Contains(s))
{
line = ;
write = false ;
}
}
// TODO写行
if (write)
csvwrite.WriteLine(line);
}
// close streams
csv.Close() ;
csvwrite.Close();
return 确定 ;
}
catch (例外情况)
{
csvwrite.Close();
csv.Close();
return ex.Message;
}
}
服务器上的程序达到创建第一个带0字节的csv的程度但随后它冻结了。它已经以管理员身份运行。任何想法?
可能是你试图将你的错误日志写入应用程序文件夹,在Win7下这不是一个好主意 - 它是相当的尽管以管理员身份运行,但您可能没有写入权限。
尝试更改代码以将其写入更友好的位置并查看是否这可以为您提供有关问题性质的任何实际信息。
(有几个安全的地方可以做到这一点,不需要任何特殊权限 - 请参阅这里有一些想法:我应该在哪里存储我的数据? [ ^ ])
Hello everybody, i got a little Problem.
i programmed a little C# app to edit some .csv files (filter linebreaks and ending lines) to make an import into another system easier.
The programm works fine on 5 machines all win7 without any problems and completes the task within 30 seconds, but as soon as i try to run it on a win 2008 R2 server it freezes. here is the code:
The filepath are read through a txt file to make it more flexible in case the source or destination folders change. As well as a txt file for the line that need to be removed at the end of the csv.
Main form (using a timer)
private void timer1_Tick(object sender, EventArgs e)
{
try
{
//todo Read Configfile
string sourcePath = Application.StartupPath + "\\SourceFolder.txt";
string targetPath = Application.StartupPath + "\\TargetFolder.txt";
srSource = new StreamReader(sourcePath);
srTarget = new StreamReader(targetPath);
string target="";
while (!srTarget.EndOfStream)
{
target = srTarget.ReadLine();
}
//testing
writeError(System.DateTime.Now.ToString() + " ->Starting write Target: " + target);
// TEST IS OK. File is written
while (!srSource.EndOfStream)
{
string line = srSource.ReadLine();
string[] filePaths = Directory.GetFiles(line, "*.csv");
foreach (string file in filePaths)
{
try
{
FileInfo fi = new FileInfo(file);
ChristmasCards ccrd = new ChristmasCards(Application.StartupPath + "\\ConfigEndings.txt"); // Conditions for ending lines (each line is a line that needs to be removed)
if (ccrd.OK)
{
DirectoryInfo dir = new DirectoryInfo(target);
if (!dir.Exists)
{
dir.Create();
}
FileInfo destfi = new FileInfo(target+"\\" + fi.Name);
writeError(System.DateTime.Now.ToString() + " ->Readinfo Target " + target+"\\"+fi.Name); // write ok!
if (destfi.Exists)
{
destfi.Delete();
}
//do process
string result = ccrd.ReadCSV(file, target + "\\" + fi.Name);
//Check if everything saved ok
if (result == "OK")
writeError(System.DateTime.Now.ToString() + " -> " +"\r\nFile Saved as: " + target + "\\" + fi.Name);
else
writeError(System.DateTime.Now.ToString() + " -> " +"\r\nError File saving: " + result);
}
else
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError Configfile not readable");
srSource.Close();
}
}
catch (Exception ex)
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError:" + ex.Message);
srSource.Close();
}
}
}
srSource.Close();
srTarget.Close();
}
catch (Exception ex)
{
writeError(System.DateTime.Now.ToString() + " -> " + "\r\nError:" + ex.Message);
}
finally
{
this.Close();
Application.Exit();
}
}
private void writeError(string message)
{
StreamWriter errorwriter = new StreamWriter(Application.StartupPath + "\\Errors.txt", true, System.Text.Encoding.Default);
errorwriter.WriteLine(System.DateTime.Now.ToString()+" -> "+ message);
errorwriter.Close();
}
Christmas card class function
StreamReader conditionsreader;
StreamReader csv;
StreamWriter csvwrite;
bool ok = false;
ArrayList cond = new ArrayList();
#region constructor
public ChristmasCards(string configfile)
{
try
{
conditionsreader = new StreamReader(configfile);
while (!conditionsreader.EndOfStream)
{
cond.Add(conditionsreader.ReadLine());
}
conditionsreader.Close();
ok = true;
}
catch (Exception ex)
{
ok = false;
}
}
public string ReadCSV(string filename, string destfilename)
{
try
{
bool nextline = false;
bool write = true;
// open the file "data.csv" which is a CSV file with headers
csv = new StreamReader(filename, System.Text.Encoding.Default);
csvwrite = new StreamWriter(destfilename, true, System.Text.Encoding.Default);
while (!csv.EndOfStream)
{
// remove line breaks
string line = csv.ReadLine();
if (!line.EndsWith("\""))
{
nextline = true;
while (nextline)
{
line += csv.ReadLine();
if (line.EndsWith("\""))
nextline = false;
}
}
//filter ending lines
foreach (string s in cond)
{
if (line.Contains(s))
{
line = "";
write = false;
}
}
//TODO Write the line
if (write)
csvwrite.WriteLine(line);
}
//close streams
csv.Close();
csvwrite.Close();
return "OK";
}
catch (Exception ex)
{
csvwrite.Close();
csv.Close();
return ex.Message;
}
}
The program on the server reaches the point where it creates the first csv with 0 bytes but then it freezes. and its already running as Administrator. any ideas?
It may be that you are trying to write your error log to the application program folder, which under Win7 is not a good idea - it is quite possible that you do not have write permission to do so, despite running as admin.
Try changing the code to write it in a more "friendly" location and see if that gives you any actual information as to the nature of the problem.
(There are several "safe" places to do that that do not require any special permissions - see here for a few ideas: Where should I store my data?[^])
这篇关于Win7 CSV编辑程序在Win Server 2008下无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!