Click here to Skip to main content
15,895,777 members
Home / Discussions / C#
   

C#

 
Questionsdk Pin
RAJESHSELVARAJ28-Feb-13 23:04
RAJESHSELVARAJ28-Feb-13 23:04 
AnswerRe: sdk Pin
Pete O'Hanlon28-Feb-13 23:24
mvePete O'Hanlon28-Feb-13 23:24 
AnswerRe: sdk Pin
Dave Kreskowiak1-Mar-13 2:21
mveDave Kreskowiak1-Mar-13 2:21 
QuestionIssue using the StreamWritter Class Pin
TwiztedFreek28-Feb-13 13:16
TwiztedFreek28-Feb-13 13:16 
AnswerRe: Issue using the StreamWritter Class Pin
TwiztedFreek28-Feb-13 13:18
TwiztedFreek28-Feb-13 13:18 
AnswerRe: Issue using the StreamWritter Class Pin
Garth J Lancaster28-Feb-13 14:08
professionalGarth J Lancaster28-Feb-13 14:08 
AnswerRe: Issue using the StreamWritter Class Pin
Jegan Thiyagesan28-Feb-13 23:33
Jegan Thiyagesan28-Feb-13 23:33 
AnswerRe: Issue using the StreamWritter Class Pin
Keith Barrow1-Mar-13 1:06
professionalKeith Barrow1-Mar-13 1:06 
The reason the code is failing is because the following is happening:


  1. You create the TestFile2.txt the first MainSalesForm is instantiated. Subsequent instantitations of MainSalesForm will overwrite the file effectively blanking it. (try prepenging the DateTime.Now.Ticks to the filename to see when the file is actually being created/overwritten).
  2. On button click, you write to the file and close the stream so the file can no longer be written to. If the CreateFile is executed later (say the form is closed and reopened) it will overwrite what has been written.


I suggest the following code:

C#
private void summaryButton_Click(object sender, EventArgs e)
{
     WriteToFile(string.Format("This is a test {0}", todayDate.ToString()); 
}


private void WriteToFile(string fileName, string content)
{
    FileInfo fi = new FileInfo(fileName);
    // Check that the file exists first
    if (!fi.Exists)
    {
        using(StreamWriter testFile = File.CreateText(fileName)
        {
            sw.Write(content);
        }
    }
    else
    {
        using (StreamWriter sw = new StreamWriter(fileName, true))
        {
            sw.Write(content);
        }
    }
}


Once the button is pressed the file will be created (if it does not exist) or will be appeneded to with the string passed to the method. Note that the file is not locked between clicks (so you can open it for reading and writing). This may not be the desired behaviour, if not the code will be unsuitable. The code I have provided may not compile (I haven't tested it) and could be tidied a little but it should get you on track.

AnswerRe: Issue using the StreamWritter Class Pin
Defens931-Mar-13 9:35
Defens931-Mar-13 9:35 
QuestionCopy username from text file to website Pin
Muhammad Sayyam28-Feb-13 2:17
Muhammad Sayyam28-Feb-13 2:17 
AnswerRe: Copy username from text file to website Pin
DaveyM6928-Feb-13 3:07
professionalDaveyM6928-Feb-13 3:07 
GeneralRe: Copy username from text file to website Pin
Richard MacCutchan28-Feb-13 3:23
mveRichard MacCutchan28-Feb-13 3:23 
GeneralRe: Copy username from text file to website Pin
DaveyM6928-Feb-13 10:30
professionalDaveyM6928-Feb-13 10:30 
GeneralRe: Copy username from text file to website Pin
Muhammad Sayyam2-Mar-13 20:19
Muhammad Sayyam2-Mar-13 20:19 
GeneralRe: Copy username from text file to website Pin
DaveyM695-Mar-13 3:08
professionalDaveyM695-Mar-13 3:08 
GeneralRe: Copy username from text file to website Pin
DaveyM697-Mar-13 21:51
professionalDaveyM697-Mar-13 21:51 
Questiontooth numbers Pin
Jassim Rahma28-Feb-13 0:35
Jassim Rahma28-Feb-13 0:35 
AnswerRe: tooth numbers PinPopular
Pete O'Hanlon28-Feb-13 1:11
mvePete O'Hanlon28-Feb-13 1:11 
AnswerRe: tooth numbers Pin
GuyThiebaut28-Feb-13 2:03
professionalGuyThiebaut28-Feb-13 2:03 
QuestionVS2012, C# Office 2007 problem Pin
Daytona67527-Feb-13 9:34
Daytona67527-Feb-13 9:34 
AnswerRe: VS2012, C# Office 2007 problem Pin
Richard MacCutchan27-Feb-13 21:58
mveRichard MacCutchan27-Feb-13 21:58 
AnswerRe: VS2012, C# Office 2007 problem Pin
OriginalGriff27-Feb-13 22:40
mveOriginalGriff27-Feb-13 22:40 
Questionc# field type is less accessible than field (SOLVED!) Pin
jojoba201127-Feb-13 9:25
jojoba201127-Feb-13 9:25 
AnswerRe: c# field type is less accessible than field Pin
Jibesh27-Feb-13 9:39
professionalJibesh27-Feb-13 9:39 
AnswerRe: c# field type is less accessible than field PinPopular
Richard Deeming27-Feb-13 9:41
mveRichard Deeming27-Feb-13 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.