473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Writing strings to file

Hi

Is there any easy way to write strings into a file for debugging purposes?

Thanks

Regards
Nov 4 '08 #1
3 1730

"John" <in**@nospam.infovis.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi

Is there any easy way to write strings into a file for debugging purposes?

Thanks

Regards
You will need a reference to System.IO

Using sw As New StreamWriter(MyFileName, True) 'last parameter tells sw to
append to file

sw.WrileLine(MyText)
End Using
Nov 4 '08 #2
I suggest looking into Tracing. It's very powerful and allows you to log to
files, xml, event log and more. I wrote code that makes it even more powerful
that will appear in the next update of my open source assembly located at
http://codeplex.com/dotnettips

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://www.cafepress.com/geekmusicart.1654787045
"John" wrote:
Hi

Is there any easy way to write strings into a file for debugging purposes?

Thanks

Regards
Nov 4 '08 #3

Here is some C# code.

This can write to your computers temp directory and "pop it" in notepad.
But I would also look at a complete logging solution.

private void WriteAndOrPopDebugMessage(string msg)
{
bool popTextFile = false;

bool generateTempFile = false;

//set those values to config file settings
popTextFile =true;//better to read from config file
generateTempFile =true;//better to read from config file

if (popTextFile || generateTempFile )
{
string fileName = WriteToTempFile(string.Empty, msg);
if (popTextFile)
{
System.Diagnostics.Process.Start(fileName);
}
else
{
Console.WriteLine(fileName);
}
}
}

public string WriteToTempFile(string prefix, string toWrite)
{

string fileName = System.IO.Path.GetTempFileName();
fileName = fileName.Replace(".tmp", ".txt");
return WriteAFile(fileName, prefix, toWrite);

}
public string WriteAFile(string fileName , string prefix, string
toWrite)
{

System.IO.StreamWriter writer = null;
System.IO.FileStream fs = null;

try
{

// Writes text to a temporary file and returns path

fs = new System.IO.FileStream(fileName,
System.IO.FileMode.Append, System.IO.FileAccess.Write);
// Opens stream and begins writing
writer = new System.IO.StreamWriter(fs);
writer.BaseStream.Seek(0, System.IO.SeekOrigin.End);
writer.WriteLine(toWrite);
writer.Flush();

return fileName;

}
finally
{
if (null != writer)
{
writer.Close();
}
if (null != fs)
{
fs.Close();
}
}
}


"John" <in**@nospam.infovis.co.ukwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi

Is there any easy way to write strings into a file for debugging purposes?

Thanks

Regards

Nov 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code...
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
2
by: Chance Ginger | last post by:
I am rather new at Python so I want to get it right. What I am doing is writing a rather large application with plenty of places that strings will be used. Most of the strings involve statements of...
1
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is...
5
by: David++ | last post by:
Hello, Just wondering how you all deal with writing large SQL strings when coding database functionailty. Some of the SQL strings can get quite long and it looks a bit messy and hard to read in...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
1
by: =?Utf-8?B?TWljaGFlbCBCbHVtZW50aGFsLCBNQ1NFLCBNQ0FE | last post by:
I am writing a .NET 1.1 C# windows service that writes to a dedicated event log (not the application event log, but one that I created). I want to make full use of all the fields in an event log...
2
by: mauricesmith42 | last post by:
Sorry i know this is rather large to be posting, but in order to understand the question you have to see all the code //#include <windows.h> //needed for opening folders #include...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.