Hey guys.
I would like to build a class that i can use to log some messages into a textfile for easier web debugging.
so i guess my question is:
How do you direct output of a string into a textfile.
thank you
11 906
Look at the System.IO namespace. Thjere is the File, FileInfo, FileStream, StreamWriter(this one in particular probably) and the StreamReader can all help you.
Hey guys.
I would like to build a class that i can use to log some messages into a textfile for easier web debugging.
so i guess my question is:
How do you direct output of a string into a textfile.
thank you
Check out the System.IO.File class for information on how to write files.
The example they list in that link is quite easy to follow (I've posted it below) -
string path = @"c:\temp\MyTest.txt";
-
if (!File.Exists(path))
-
{
-
// Create a file to write to.
-
using (StreamWriter sw = File.CreateText(path))
-
{
-
sw.WriteLine("Hello");
-
sw.WriteLine("And");
-
sw.WriteLine("Welcome");
-
}
-
}
-
-
// Open the file to read from.
-
using (StreamReader sr = File.OpenText(path))
-
{
-
string s = "";
-
while ((s = sr.ReadLine()) != null)
-
{
-
Console.WriteLine(s);
-
}
-
}
-
-
try
-
{
-
string path2 = path + "temp";
-
// Ensure that the target does not exist.
-
File.Delete(path2);
-
-
// Copy the file.
-
File.Copy(path, path2);
-
Console.WriteLine("{0} was copied to {1}.", path, path2);
-
-
// Delete the newly created file.
-
File.Delete(path2);
-
Console.WriteLine("{0} was successfully deleted.", path2);
-
}
-
catch (Exception e)
-
{
-
Console.WriteLine("The process failed: {0}", e.ToString());
-
}
-
}
-Frinny
very nice, thank you.
However, it is eazy to follow im not quite sure how to modify it to my needs.
I want to append a message to a text file, without creating a new file every time.
is there a way that i could do:
file.open(path);
file.write(message '\n');
very nice, thank you.
However, it is eazy to follow im not quite sure how to modify it to my needs.
I want to append a message to a text file, without creating a new file every time.
is there a way that i could do:
file.open(path);
file.write(message '\n');
Look at the overloads to File.Open(), there are some choices there for append and such.
do you know what the options are for FileMode?
never mind, stupid question.
Do i need to make a FileStream? what is that?
or should i be able to just file.open?
it also seems like it only wants to accept a byte[]. If i have a simple string, how do i change that?
You can create a StreamWriter with a FileStream.
StreamWriter's constructor can take in a stream (FileStream is a stream) so you can use a StreamWriter instead.
Thank you, for your help.
Im still lost, but let me play around for a little bit. I will post another question in due time
if you could give me some code to try that would be great, lets say i have a string message = "I like Cheese";
and i want to have a class, with a function that will send that string to a text file. My code so far (Not working): -
public class LogAMessage
-
{
-
string path = @"\\server\test.txt";
-
public LogAMessage()
-
{
-
//
-
// TODO: Add constructor logic here
-
//
-
}
-
public static void logAMessage(string message)
-
{
-
try
-
{
-
FileStream s2 = new FileStream(path, FileMode.Append, FileAccess.Write);
-
s2.Write(message, 0, message.Length);
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
}
-
Need an extra little step -
public static void logAMessage(string message)
-
{
-
try
-
{
-
FileStream s2 = new FileStream(path, FileMode.Append, FileAccess.Write);
-
StreamWriter sw = new StreamWriter(s2);
-
sw.Write(message);
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
}
-
logfile.txt gets created, but is empty -
public static void logAMessage(string message)
-
{
-
string path = "c:\\logfile.txt";
-
try
-
{
-
FileStream s2 = new FileStream(path, FileMode.Append);
-
StreamWriter sw = new StreamWriter(s2);
-
sw.Write(DateTime.Now);
-
sw.Write(" :: ");
-
sw.Write(message);
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Murat Tasan |
last post by:
hi, i am having a small problem with constructing an inner class.
i am using an inner class (and not a static nested class) because the
methods of the inner class need access to the enclosing...
|
by: Fernando Rodriguez |
last post by:
Hi,
I need to traverse the methods defined in a class and its superclasses. This
is the code I'm using:
# An instance of class B should be able to check all the methods defined in B
#and A,...
|
by: Gabriel Genellina |
last post by:
Hi
In the following code sample, I have:
- a Worker class, which could have a lot of methods and attributes. In
particular, it has a 'bar' attribute. This class can be modified as needed.
- a...
|
by: Robert |
last post by:
Python doesn't know the class of a method when container not direct
class attribute:
>>> class X:
.... def f():pass
.... g=f
.... l=
....
>>> X.g
<unbound method X.f>
|
by: Oplec |
last post by:
Hi,
I'm learning C++ as a hobby using The C++ Programming Language : Special
Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that
deal with templates. Exercise 13.9 asks for me...
|
by: Banaticus Bart |
last post by:
I wrote an abstract base class from which I've derived a few other
classes. I'd like to create a base class array where each element is
an instance of a derived object. I can create a base class...
|
by: Andy |
last post by:
Hi all,
I have a site with the following architecture:
Common.Web.dll - Contains a CommonPageBase class which inherits
System.Web.UI.Page
myadd.dll - Contains PageBase which inherits...
|
by: Hamilton Woods |
last post by:
Diehards,
I developed a template matrix class back around 1992 using Borland C++ 4.5
(ancestor of C++ Builder) and haven't touched it until a few days ago. I
pulled it from the freezer and...
|
by: emin.shopper |
last post by:
I had a need recently to check if my subclasses properly implemented
the desired interface and wished that I could use something like an
abstract base class in python. After reading up on metaclass...
|
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...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |