I am trying to write some strings to a text file on the formclosing event. The problem is that the streamwriter doesn't write anything, it just writes a blank slate. I have 2 different text files, the first one will log all of the graph data and the second text file will log a couple of preferences relevant to my application. My code is shown below for both the closing event and a seperate workhorse method:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason.Equals(CloseReason.WindowsShutDown) || (e.CloseReason.Equals(CloseReason.UserClosing)))
{
if (MessageBox.Show("You are closing this application.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
{
writeContents("Interrupted");
return;
}
else
e.Cancel = true;
}
}
private void writeContents(string status)
{
//---writes the graph data-----
TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt");
twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--");
//writes the table of values in there, assume x and y are the same size arrays
for(int i = 0; i < x.Count; i++)
{
twBackupData.WriteLine(x[i] + " " + y_lower[i] + " " + y_upper[i]);
}
//---writes some of the preferences------
TextWriter twBackupDataInfo = new StreamWriter("C://springTestBackupInfo.txt");
twBackupDataInfo.WriteLine(status);
twBackupDataInfo.WriteLine(cycleCount.ToString());
twBackupDataInfo.WriteLine(section.ToString());
twBackupDataInfo.WriteLine(revsPerCycle.ToString());
twBackupDataInfo.WriteLine(preturns.ToString());
twBackupDataInfo.WriteLine(direction.ToString());
}
If you can provide advice or help me find out why it's writing blanks I would greatly appreciate it. Thank you!
You need to close the StreamWriter using the using statement.
It's much easier to just use:
var linesToWrite = new list<string>();
linesToWrite.Add(status);
linesToWrite.Add(cycleCount.ToString());
...
File.WriteAllLines("C://springTestBackupData.txt", linesToWrite);
You need to close/dispose the writer for it to write, otherwise it never flushes its stream (i.e. writes the data to the file)
Using the 'using' statement automatically disposes of an object when it goes out of scope so:
using(TextWriter twBackupData = new StreamWriter("C://springTestBackupData.txt"))
{
// Do your stuff here - write to the tw ---
twBackupData.WriteLine("--Cycle#-- --TorqueLower-- --TorqueUpper--");
//writes the table of values in there, assume x and y are the same size arrays
for(int i = 0; i < x.Count; i++)
{
twBackupData.WriteLine(x[i] + " " + y_lower[i] + " " + y_upper[i]);
}
}
Will ensure your file gets written to
More info here:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
You need to do .Close() on your StreamWriters;
Related
I have an Error if I write something in a newly created File.
This is my code:
private void ButtonClick(object sender, EventArgs e)
{
Button b = (Button)sender;
string inputKey = b.Text;
for (int i = 0; i < tunes.Length; i++)
{
if (b.Text == tun[i].TuneName)
{
Console.Beep(tun[i].Frequency, 200);
Input.Items.Add(b.Text);
Output.Items.Add(tun[i].TuneName);
if (startButtonPressed == true)
{
filename2 = musicFileName + ".csv";
File.WriteAllText(filename2, tun[i].TuneName);
RecordList.Items.Add(tun[i].TuneName);
}
}
}
}
The Error comes at Line : File.WriteAllText()...
It says that the File can not be used, because it's used by an another process,but I havent opened any File.
I'd use a Filestream generated by File.Create(), but I'd make the loop inside the using statement, so you ensure that all ressources will be released at the end (that's why you use using).
using (FileStream fs = File.Create(Path.Combine(musicFileName, ".csv")))
{
foreach (tun in tunes)
{
fs.Write(tun.TuneName);
}
}
The problem you were actually having is, that you were never closing your file. You should look up using-keyword. It can used only with classes implementing the IDisponsable Interface. It then will call disponse() at the end of the using block and all ressources will be released, eg the file will be closed.
You need to make sure that the variable filename2 contains a valid path like C:\temp\myfile and not just myfile additionally you might need to run visual studio with elevated privilege if the location is not accessible otherwise.
You could also use streamwriter...
using (StreamWriter writer =new StreamWriter(musicFileName + ".csv";))
{
writer.Write(tun[i].TuneName);
}
I have this piece of code written in C# (it's part of implementation of ID3 algorithm)
public static void printNode(TreeNode root, string tabs, ref StreamWriter xa)
{
//Console.ReadKey();
Console.WriteLine(tabs + '|' + root.attribute + '|');
if (root.attribute.values != null)
{
for (int i = 0; i < root.attribute.values.Length; i++)
{
Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
xa.WriteLine(tabs + "\n" + root.attribute.values[i] +"\n");
TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
printNode(childNode, "\t" + tabs, ref xa);
}
}
}
I also have this declaration.
StreamWriter xa = new StreamWriter("tree1.txt");
And issue is that after running code file 'tree1.txt' is always empty. Any idea how to handle this? As you can see I tried references cause it seems to me like right course of action but I don't really know much about C# and recurrence function behaviour.
You need to close the StreamWriter to force it to flush to disk.
You also need to look in the current directory, which might not be what you think it is.
i have a C# form having troubles with file operations.
Here's how it works :
When the user click on the "start" button, the program begins.
It opens the file (if exists?), check the header of this file and modify a boolean if this header exists.
Then, it opens the file again, to put a header (if non exists) and other infos, or just infos (if header exists)
Here's the code :
public bool enteteExiste = false;
private void start_Click(object sender, EventArgs e)
{
try
{
verifieEntete();
//INSERTION DE L'ENTETE DU FICHIER CSV
writeToCsv = new StreamWriter(boxFilePath.Text + "\\" + filename, true);
canAcces = true;
}
catch (Exception)
{
MessageBox.Show("Droits d'accès au dossier insuffisant OU fichier déjà ouvert" + Environment.NewLine + "Assurez vous d'avoir fermé le fichier et de disposer des droits requis" + Environment.NewLine + "Arrêt de la procédure");
}
}
public void verifieEntete()
{
string absolutFilePath = boxFilePath.Text + '\\' + filename;
String[] fileContent = File.ReadAllText(absolutFilePath).Split(',');
for (int i = 0; i < fileContent.Length; i++)
if (fileContent[i].Contains("MAC;SERIAL;IP;MODELE;MODULE-EXT"))
enteteExiste = true ;
}
When file already exists, program runs perfectly,
When file does not exists, program goes into catch Exception.
Is ReadAllTest() not supposed to check wether file exists or not ?
Should i add a special Exception catch "filenotfound", and create it ?
The MSDN docs for File.ReadAllText state explicitly that it will throw a FileNotFoundException if the file doesn't exist. So yes, you have to explicitly check for its existence.
It's best not to rely on Exceptions for something that can easily be checked beforehand. Both because of potential performance issues (catching an exception is a lot slower than a simple if test), and both for code clarity and readability - an if/else branching is usually easier to understand and structure than a try/catch block. This way you can handle the error before it happens, and fix it (like, say, creating the file if necessary)
public void verifieEntete()
{
string absolutFilePath = boxFilePath.Text + '\\' + filename;
if (!File.Exists(absolutFilePath) // <--- ADD EXPLICIT CHECK
{
// Create the file here.
}
// Now we know the file is *sure* to exist, because we handled it
// explicitly.
String[] fileContent = File.ReadAllText(absolutFilePath).Split(',');
for (int i = 0; i < fileContent.Length; i++)
if (fileContent[i].Contains("MAC;SERIAL;IP;MODELE;MODULE-EXT"))
enteteExiste = true ;
}
Will try to post only relevant code as my program is quite large already. Basically the program adds customer information into an arraylist-struct. I've got the storing and saving and file loading working flawlessly, but when I'm trying to show the data I'm getting exceptions.
Most of the main code is on a class seperate to the forms, and this particular call comes from "frmViewRecords".
public void ViewData(int currentRecord)
{
string fn = ((custDetails)datalist[currentRecord]).firstName;
frmViewRecords viewRecords = new frmViewRecords();
viewRecords.WriteData(fn);
}
The above code is what causes the exception, but the code for the messagebox below works fine.
public void LoadData()
{
bool fileLoaded = false;
//Load the database
try
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); //Create the filestream
try
{
BinaryFormatter bf = new BinaryFormatter(); //New binary formatter for deserialization
datalist = (ArrayList)bf.Deserialize(fs);
fileLoaded = true; //Update the fileLoaded bool so that it doesn't open the file dialog instance
recordCount = datalist.Count;
MessageBox.Show("" + filename + " loaded successfully."); //Inform the user that the file was automatically loaded
MessageBox.Show("Test: " + ((custDetails)datalist[0]).firstName);
}
catch
{
MessageBox.Show("Could not de-serialise from " + filename, "FILE LOADING PROBLEM", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
fs.Close();
}
catch
{
if (MessageBox.Show("File isn't in the right location, this is normal if a dataset does not yet exist.\n\n If the file exists elsewhere click no and you will be prompted to find the database file, else click yes to create a default file.", "FILE LOADING PROBLEM", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
fileLoaded = true;
CreateDefaultData();
}
}
I have tried 'string fn = ((custDetails)datalist[0]).firstName;' to ensure it isn't the variable causing the problems, and the exception still happens. I am pretty much out of ideas. The problem cannot be with struct or arraylist definition as the the messagebox in LoadData() works fine and outputs the correct information. I tried moving the messagebox to the ViewData method and that also began giving an exception so I can only assume something is wrong with my method?
These methods are on "MainClass.cs" and below is how I called the method from frmViewRecords:
MainClass mainClass = new MainClass();
int currentRecord = 0;
private void LoadData()
{
mainClass.ViewData(currentRecord);
}
It might be worth mentioning that previously, I was calling the data straight from frmViewRecords like so:
txtFirstName.Text = ((MainClass.custDetails)mainClass.datalist[currentRecord].firstName;
But after getting the same exception while the messagebox prompt worked, I rewrote it to the above and still I get the problem so I have no idea what is causing it.
There are no items in the datalist. Probably the value of the recordCount in the LoadData is zero as well. Try this:
if(datalist.Count != 0) { /* Get the current record */ }
i am very new to C#, and this is my first question, please be gentle on me
I am trying to write a application to capture some tick data from the data provider, below is the main part of the program
void zf_TickEvent(object sender, ZenFire.TickEventArgs e)
{
output myoutput = new output();
myoutput.time = e.TimeStamp;
myoutput.product = e.Product.ToString();
myoutput.type = Enum.GetName(typeof(ZenFire.TickType), e.Type);
myoutput.price = e.Price;
myoutput.volume = e.Volume;
using (StreamWriter writer = File.AppendText("c:\\log222.txt"))
{
writer.Write(myoutput.time.ToString(timeFmt) + ",");
writer.Write(myoutput.product + "," );
writer.Write(myoutput.type + "," );
writer.Write(myoutput.price + ",");
writer.Write(myoutput.volume + ",");
}
i have successfully write the data into the text file, however i know that this method will be call like 10000 times a second during peak time, and open a file and append it many times a second is very inefficient, i was pointed to use a buffer or some sort, but i have no idea how to do it, i try reading the document but i still dont understand, thats why i turn in here for help.
Please give me some (working) snippet code so i can pointed to the write direction. thanks
EDIT: i have simplified the code as much as possible
using (StreamWriter streamWriter = File.AppendText("c:\\output.txt"))
{
streamWriter.WriteLine(string.Format("{0},{1},{2},{3},{4}",
e.TimeStamp.ToString(timeFmt),
e.Product.ToString(),
Enum.GetName(typeof(ZenFire.TickType), e.Type),
e.Price,
e.Volume));
}
ED has told me to make my stream to a field, how is the syntax looks like? can anyone post some code to help me? thanks a lot
You need to create a field for the stream instead of a local variable. Initialize it in constructor once and don't forget to close it somewhere. It's better to implement IDisposable interface and close the stream in Dispose() method.
IDisposable
class MyClass : IDisposable {
private StreamWriter _writer;
MyClass() {
_writer = File.App.....;
}
void zf_TickEvent(object sender, ZenFire.TickEventArgs e)
{
output myoutput = new output();
myoutput.time = e.TimeStamp;
myoutput.product = e.Product.ToString();
myoutput.type = Enum.GetName(typeof(ZenFire.TickType), e.Type);
myoutput.price = e.Price;
myoutput.volume = e.Volume;
_writer.Write(myoutput.time.ToString(timeFmt) + ",");
_writer.Write(myoutput.product + "," );
_writer.Write(myoutput.type + "," );
_writer.Write(myoutput.price + ",");
_writer.Write(myoutput.volume + ",");
}
public void Dispose() { /*see the documentation*/ }
}
There are many things you can do
Step 1. Make sure you don't make many io calls and string concatenations.
Output myOutput = new Outoput(e); // Maybe consruct from event args?
// Single write call, single string.format
writer.Write(string.Format("{0},{1},{2},{3},{4},{5}",
myOutput.Time.ToString(),
myOutput.Product,
...);
This I recommend regardless of what your current performance is. I also made some cosmetic changes (variable/property/class name casing. You should look up the difference between variables and properties and their recommended case etc.)
Step 2. Analyse your performance to see if it does what you want. If it does, no need to do anything further. If performance is still too bad, you can
Keep the file open and close it when your handler shuts down.
Write to a buffer and flush it at regular intervals.
Use a logger framework like log4net that internally handles the above for you, and takes care of hairy issues like access to the log file from multiple threads.
I would use String.Format:
using (StreamWriter writer = new StreamWriter(#"c:\log222.txt", true))
{
writer.AutoFlush = true;
writer.Write(String.Format("{0},{1},{2},{3},{4},", myoutput.time.ToString(timeFmt),
myoutput.product, myoutput.type, myoutput.price, myoutput.volume);
}
If you use # before string you don't have to use double \.
This is much faster - you write only once to the file instead of 5 times. Additionally you don't use + operator with strings which is not the fastest operation ;)
Also - if this is multithreading application - you should consider using some lock. It would prevent application from trying to write to the file from eg. 2 threads at one time.