2015-10-16 108 views
1

如果每个项目的radioButton为“是”,我有一个基于Repeater控件中的项目创建的文件。我的问题是,如果文件是空的,它仍然在创建。我试过FileName.Length> 0和其他可能的解决方案,但我得到的错误,该文件无法找到。我相信这个问题是在我的逻辑之内,但我不知道在哪里。有任何想法吗?如何防止创建空文件

protected void btnContinue_Click(object sender, EventArgs e) 
{ 
    string JobName; 
    string FileName; 

    StreamWriter sw; 
    string Name, Company, Date; 

    JobName = TYest + "_" + System.DateTime.Now; 
    JobName = JobName.Replace(":", "").Replace("/", "").Replace(" ", ""); 
    FileName = JobName + ".txt"; 

    sw = new StreamWriter(C: +"/" + FileName, false, Encoding.GetEncoding(1250)); 

    foreach (RepeaterItem rpItems in rpGetData.Items) 
    { 
     RadioButtonList rbYesNo = (RadioButtonList)rpItems.FindControl("rbBadge"); 

     if (rbYesNo.SelectedItem.Text == "Yes") 
     { 
      Label rName = (Label)rpItems.FindControl("lblName"); 
      Label rCompany = (Label)rpItems.FindControl("lblCompany"); 
      Label rFacilityName = (Label)rpItems.FindControl("lblFacility_Hidden"); 
      Name = rName.Text; 
      Company = rCompany.Text; 
      Date = System.DateTime.Now.ToString("MM/dd/yyyy"); 

      sw.WriteLine("Name," + Name); 
      sw.WriteLine("Company," + Company); 
      sw.WriteLine("Date," + Date); 
      sw.WriteLine("*PRINTLABEL"); 
     } 

     sw.Flush(); 
     sw.Dispose(); 

     if (File.Exists("C:/" + FileName)) 
     { 
      try 
      { 
       File.Copy(+"C:/" + FileName, LoftwareDropPath + FileName, true); 
      } 
      catch (Exception ex) 
      { 
       string msgE = "Error"; 
       msgE += ex.Message; 
       throw new Exception(msgE); 
      } 
     } 
     else 
     { 
      //Do something if temp file not created properly 
      lblMessage.Text = "An error has occurred. Plese see your host to get a printed name badge."; 
     } 

     MessageBox messageBox = new MessageBox(); 
     messageBox.MessageTitle = "Printed?"; 
     messageBox.MessageText = "If not, please see host."; 
     Literal1.Text = messageBox.Show(this); 
    } 
} 
+0

你看过可能创建文件的StreamWriter构造函数吗?另外,FileName.Length正在检查字符串,而不是文件本身,因为在任何地方你都没有一个变量是文件本身,而是你正在使用文件类中的方法。 –

回答

2

听起来像要检测文件是否为空。用途:

long length = new System.IO.FileInfo(path).Length; 
if(length == 0).... 

FileName.Length只是告诉你的文件名是多久 - 不usefule

0

如果文件存在第一为什么不签?这应该解决你的异常问题!如果您想知道文件是否为空,我会建议您检查一下您正在写入的文件,并确保它并非全部为空,并且如果您确实有内容,请写入该文件?

if(File.Exists(File)) 
{ 
    if(new FileInfo(File).Length > 0) 
    { 
     //Do Stuff. 
    } 
} 
0

您应该将其更改为仅在写入一些数据时才写入和创建文件。

这样做的一个简单方法是存储的一切记忆的东西,如一个StringBuilder,然后事后字符串生成器的内容写入文件,如果有东西可写:这个怎么样

var sb = new StringBuilder(); 

foreach (RepeaterItem rpItems in rpGetData.Items) 
{ 
    RadioButtonList rbYesNo = (RadioButtonList)rpItems.FindControl("rbBadge"); 

    if (rbYesNo.SelectedItem.Text == "Yes") 
    { 
     // ..omitted.. 

     sb.AppendLine("Name," + Name); 
     sb.AppendLine("Company," + Company); 
     sb.AppendLine("Date," + Date); 
     sb.AppendLine("*PRINTLABEL"); 
    } 
} 

if (sb.Length > 0) 
{ 
    File.WriteAllText(FileName, sb.ToString(), Encoding.GetEncoding(1250)); 
} 
0

StreamWriter sw = null; 
string Name, Company, Date; 

JobName = TYest + "_" + System.DateTime.Now; 
JobName = JobName.Replace(":", "").Replace("/", "").Replace(" ", ""); 
FileName = @"C:\" + JobName + ".txt"; 
try 
{ 
foreach (RepeaterItem rpItems in rpGetData.Items) 
{ 

    RadioButtonList rbYesNo = (RadioButtonList)rpItems.FindControl("rbBadge"); 

     if (rbYesNo.SelectedItem.Text == "Yes") 
     { 
      if (null == sw) 
       sw = new StreamWriter(FileName, false, Encoding.GetEncoding(1250)); 
      Label rName = (Label)rpItems.FindControl("lblName"); 
      Label rCompany = (Label)rpItems.FindControl("lblCompany"); 
      Label rFacilityName = (Label)rpItems.FindControl("lblFacility_Hidden"); 
      Name = rName.Text; 
      Company = rCompany.Text; 
      Date = System.DateTime.Now.ToString("MM/dd/yyyy"); 

      sw.WriteLine("Name," + Name); 
      sw.WriteLine("Company," + Company); 
      sw.WriteLine("Date," + Date); 
      sw.WriteLine("*PRINTLABEL"); 
    } 
} 
finally 
{ 
    if (null != sw) 
    { 
     sw.Flush(); 
     sw.Dispose(); 
    } 
} 

一次构建您的FileName,以便您知道它始终是相同的。然后只有在写入内容时才创建StreamWriter。此外,请使用try..finally来确保您的代码释放您的资源总是被击中。

0

您可以检查所有项目是否有资格开流作家喜欢在此之前节省:

var itemsToBeSaved = rpGetData.Items 
    Where(ri => ((RadioButtonList)ri.FindControl("rbBadge")).SelectedItem.Text == "Yes"); 

if (itemsToBeSaved.Any()) { 
    string path = @"C:\" + FileName; 
    using (var sw = new StreamWriter(path, false, Encoding.GetEncoding(1250))) { 
     foreach (RepeaterItem rpItems in itemsToBeSaved) { 
      Label rName = (Label)rpItems.FindControl("lblName"); 
      Label rCompany = (Label)rpItems.FindControl("lblCompany"); 
      Label rFacilityName = (Label)rpItems.FindControl("lblFacility_Hidden"); 
      Name = rName.Text; 
      Company = rCompany.Text; 
      Date = System.DateTime.Now.ToString("MM/dd/yyyy"); 

      sw.WriteLine("Name," + Name); 
      sw.WriteLine("Company," + Company); 
      sw.WriteLine("Date," + Date); 
      sw.WriteLine("*PRINTLABEL"); 
     } 
    } // Flushes, Closes und Disposes the stream automatically. 
} 

第一条语句准备的只包含那些中继项的过滤枚举进行保存。 itemsToBeSaved.Any()测试此枚举是否至少包含一个项目。此枚举然后在foreach语句中重用。因此没有必要再次检查条件。

即使在写入文件时发生异常,using语句也会在所有情况下关闭流。我还在using语句中声明了流编写器。因此您可以删除您的声明StreamWriter sw = null;

还注意到表达式@"C:\" + FileName@使字符串常量为逐字串。这意味着通常的转义字符'\'失去其含义,并且如那样使用Path.Combine(...)在这里不起作用,因为它不会在驱动器号后添加路径分隔符。