2015-10-16 107 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(...)在這裏不起作用,因爲它不會在驅動器號後添加路徑分隔符。