We've created a desktop timer that our users are using to track their daily tasks and projects. It outputs data to a .csv file when they close the application. Occasionally they are going to need to manually update the csv file to either take time off or add time. When they have been doing this in the current state after they save all the columns are combined into column A. I am unclear on what is causing this and tried to research encoding but couldn't find anything I could relate to this scenario.
Full form1.cs: full form1.cs code
Code related to csv:
//event handler for the closing event -- the output dump is here so the timer can be used all day and captures even on an accidetnal close
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//if the file exists we don't need to write the headers so we can skip the additional line write and just append the data created
if (File.Exists(dskPath + "\\" + agentName + ".csv"))
{
using (var file = new StreamWriter(dskPath+"\\" + agentName + ".csv", true, Encoding.UTF8))
{
foreach (var data in TimerData)
{
file.WriteLine(string.Join(",", data.agentID, data.agentStatus, data.statusTime, data.startTime, data.currentDate, data.hydraTask, data.clientname, data.publishdate, data.notes));
}
file.Close();
notifyIcon1.Icon = null;
MessageBox.Show("The file for " + agentName + " has been written to G:\\Communicator Ops\\Population Health\\Pop Process\\Time Tracking\\User Data.", "Application Closed");
}
}
//if the file hasn't been created before we can drop the headers in for the columns
else
{
using (var file = new StreamWriter(dskPath +"\\"+ agentName + ".csv", true, Encoding.UTF8))
{
file.WriteLine(string.Join(",", "Agent ID", "Agent Status", "Status Time", "Start Time", "Current Date", "hydra task number", "Client Name", "Publish Date", "Notes"));
foreach (var data in TimerData)
{
file.WriteLine(string.Join(",", data.agentID, data.agentStatus, data.statusTime, data.startTime, data.currentDate, data.hydraTask, data.clientname, data.publishdate, data.notes));
}
file.Close();
notifyIcon1.Icon = null;
MessageBox.Show("The file for " + agentName + " has been written to your desktop.","Application Closed");
}
}
}
I have a theory about what might be occurring. The C# code looks OK. I think the users are opening the file in a way you were not expecting. I think they are doing the following:
1) Opening Excel and then navigating to the file location. This will cause this dialog to open:
2) They hit Next and get to step 2.
At this step they may be leaving Tab selected as the delimiter which is NOT correct. This is where they may be causing your problem.
They hit Next again
3)
Finally they hit Finish and everything ends up in column A.
If I double click on the desktop icon it seems to work OK. If I select comma as the delimiter, and uncheck Tab, that worked too.
That is my guess. Hopefully this will help you out.
Related
Keep in mind i am using services not Windows Form App.
When I try to use System.Windows.Forms.Clipboard.GetText(); my code
don't go to next step.
means System.Windows.Forms Controls are deprecated
I am Trying to get copied text from Clipboard and change the text then set back to clipboard.
Please Help me.
below is my code
public void working()
{
while (true)
{
// Get text from clipboard
string clipboardtext = System.Windows.Forms.Clipboard.GetText();
string path = "c:\\sample.txt";
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(string.Format("Clip Board Test " + "-" + " " + "Windows Service is called on " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm:ss") + ""));
writer.Close();
System.Windows.Forms.Clipboard.SetDataObject("Hello ClipBoard");
}
Thread.Sleep(ScheduleTime * 6000);
}
}
Windows service is not running in same "scope" as your user GUI, it cannot access to your clipboard or key stroke or anything you do. Services are useful, because they can run without logged user or under different user.
Services are not allowed to access your keystrokes or clipboard also for security reasons.
So I am currently trying to do this question for tafe but am having trouble getting it to add text to the file after the first time.
"Create an application that at start up asks the user to choose an output file using a Save File Dialog Window. The application to have a “Write to File” button that when clicked will take a name and age that the user has entered into two text boxes and write them directly to the file. The user can repeat the button click action as often as they wish."
This is my current code for it:
private void Form4_Load(object sender, EventArgs e)
{
saveFileDialog1.Title = ("Choose Save Location");
saveFileDialog1.ShowDialog();
}
private void btnSendToFile_Click(object sender, EventArgs e)
{
string strName = txtName.Text;
string strAge = txtAge.Text;
string strTitles = ("Name \t\t Age");
string strCombined = strTitles + "\n" + (strName + "\t\t" + strAge);
System.IO.StreamWriter OutFile;
MessageBox.Show("The Name and Age of the Person Entered Will be Written to a File");
OutFile = System.IO.File.CreateText(saveFileDialog1.FileName);
OutFile.WriteLine(strCombined);
OutFile.Close();
MessageBox.Show("The Details Have Been Written to File" + saveFileDialog1.FileName);
StreamWriter AddFile = File.AppendText(saveFileDialog1.FileName);
AddFile.Write(strCombined);
}
I'm not sure if I should be doing a loop or not and this is the form itself, https://gyazo.com/e2c4170d46295d6f92a35026e1f2304b
Any help is appreciated, Thanks in advance
For what you're doing you absolutely don't need to work with streams or anything that low level (you don't need to call the create methods on files and use a stream returned by it). You can directly use the methods that write or append to the file and handle all of this for you.
For example using AppendAllText will do everything for you (managing the streams, flushing and closing them, appending at the end, creating the file if it doesn't exist etc etc).
so you can replace all of this code :
System.IO.StreamWriter OutFile;
MessageBox.Show("The Name and Age of the Person Entered Will be Written to a File");
OutFile = System.IO.File.CreateText(saveFileDialog1.FileName);
OutFile.WriteLine(strCombined);
OutFile.Close();
MessageBox.Show("The Details Have Been Written to File" + saveFileDialog1.FileName);
with
File.AppendAllText(saveFileDialog1.FileName, strCombined);
I have no idea what you're doing in the code that follows it (calling appendtext, i assume you're thinking you need to append after creating?) in any case you don't need it. So if you just want to append all the text of "strcombined" at the end of the file each time you click the whole function should look like :
private void btnSendToFile_Click(object sender, EventArgs e)
{
string strName = txtName.Text;
string strAge = txtAge.Text;
string strTitles = ("Name \t\t Age");
string strCombined = strTitles + "\n" + (strName + "\t\t" + strAge);
MessageBox.Show("The Name and Age of the Person Entered Will be Written to a File");
File.AppendAllText(saveFileDialog1.FileName, strCombined);
MessageBox.Show("The Details Have Been Written to File" + saveFileDialog1.FileName);
}
Also as a side comment i would rewrite all of it as such for readability, these are suggestions and not hard rules:
1) Removed hungarian notation (Don't add a prefix to your variable to indicate it's type, it's generally frowned upon)
2) Removed a lot of the temporary variables that aren't really needed (used once in the same local function)
This makes for a much smaller and very readable function:
private void btnSendToFile_Click(object sender, EventArgs e)
{
MessageBox.Show("The Name and Age of the Person Entered Will be Written to a File");
File.AppendAllText(saveFileDialog1.FileName,"Name \t\t Age"+ "\n" + txtName.Text + "\t\t" + txtAge.Text);
MessageBox.Show("The Details Have Been Written to File" + saveFileDialog1.FileName);
}
I have this question,
My boss wants a program which you can enter a path, Console.ReadLine(directory);
This is irrelevant, I got this part working. In fact, the whole code/program is working as it should be.
The point of the path is that the program scans all the files in the given directory/subdirectories for the last write time. He want to pay minimum effort in this. So the plan is to use Windows to start this program once every 24 hours.
Only problem with this "minimum effort" part is that you have to enter the path EVERYTIME when it's started. So it actually doesn't go automaticly.
The question is: is there a way to avoid this?
For example Thread.Sleep(); and when it's done sleeping goto a label right under the Console.ReadLine(directory);?
So not once a day, but sleeping for 24 hours and for 1 whole minute working?
If it's any help, here's the code:
using System.IO;
using System.Security.Permissions;
namespace CheckWithinTime
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Which folder do you wish to scan?");
string path = Console.ReadLine();
//copies everything from the console to a .txt file
FileStream filestream = new FileStream(#"C:\Logs\Log.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);
//this is the path which you type in a the beginning of the program
string[] files = Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
List<string> updatedFiles = new List<string>();
DateTime from = DateTime.Now.AddDays(-1);
DateTime to = DateTime.Now;
foreach (string name in files)
{
FileInfo file = new FileInfo(name);
string fullname = file.FullName;
//checks if the last writed time occured less then 24 hours ago, if it's not it will not be loggeed
if (file.LastWriteTime >= from && file.LastWriteTime <= to)
{
updatedFiles.Add(name);
Console.WriteLine(file.FullName + " ; " + "last changed at >> " + " ; " + file.LastWriteTime.ToString());
//Console.WriteLine("File created at >> " + file.CreationTime.ToString());
//Console.WriteLine("File last opened at >> " + file.LastAccessTime.ToString());
Console.WriteLine();
}
}
streamwriter.Close();
filestream.Close();
//The mail class basicly sends an e-mail to the server with the log file, but this is irrelevant
Mail();
}
}
}
It used to be just a simple file system watcher. After that it was like it is now, but without the Console.ReadLine() part. And now he wants to give a path.
If anyone can tell me a way to avoid the Console.ReadLine(), but only use call it when you need it. I would appreciate it!
Sorry in advance for my big texts.
The best way to do this would be to either create an XML file or use a notepad file and have windows run a task manager.
You can set up a program to run every so often in windows task manager and all you need to do is save the path to a file and have your C# console application read that file and get the path, the path will always be store and the program will run all the time.
We do this at work; a program has been running for 4 months doing through a bunch of paths and we don't edit it anymore.
Xml file/config is overkill for one setting. Pass it in to the command line string[] args:
string path;
if(args.Length > 0)
{
path = args[0];
Console.WriteLine("Using path: " + path);
}
else
{
Console.WriteLine("Which folder do you wish to scan?");
path = Console.ReadLine();
}
Then use task scheduler as already suggested, but pass the path as a command line argument. You can also then launch it with different paths without your app supporting multiple paths.
I have a program that is a launcher for a mod for a game. The launcher works for most of the people who use it, including myself, but for some there is a strange bug that really has me struggling to fix it, and further more driving me absolutely mental!
The basic idea is that my mod files are contained in a folder, the launcher iterates through these files, reads a certain byte of the file and based on the result either moves the file, or moves the file and writes some text to a certain file, then launches the game. Seemingly simple.
The main launch function looks like this:
private void Launch()
{
using (StreamWriter writer = new StreamWriter(userScriptPath, false, Encoding.Unicode))
{
foreach (string file in Directory.GetFiles(modFolderPath + "\\Files\\", "*.pack"))
{
int p = GetPackType(file);
if (p == 3)
{
if (File.Exists(modFolderPath + "\\Files\\" + Path.GetFileName(file)))
{
File.Move(modFolderPath + "\\Files\\" + Path.GetFileName(file), dataPath + "\\" + Path.GetFileName(file));
}
writer.WriteLine("mod \"" + Path.GetFileName(file) + "\";");
}
else if (p == 4)
{
if (File.Exists(modFolderPath + "\\Files\\" + Path.GetFileName(file)))
{
File.Move(modFolderPath + "\\Files\\" + Path.GetFileName(file), dataPath + "\\" + Path.GetFileName(file));
}
}
}
}
Process game = Process.Start(gamePath);
// There is code here that waits for a minute until the game process is actually found, incase its slow starting up
game.WaitForExit();
RestorePacks(); // <- Method to put back the files after finishing
}
The problem some users are getting is that when launching the mod the game launches but it appears as though the launcher doesn't move the files as the game is still in its normal state, it was very hard to ascertain exactly why this was happening as I had no way of debugging it on their computers and the program was working fine for me and all of my test users.
To try and find out what was going on I added a number of checks and some logging to the launcher so that if the launcher didn't work, I'd at least have an idea why. (Note that no errors are thrown for the users even when it doesn't work)
The checks I added included using File.Exists() after attempting to move a file to make sure it was actually moved, the logging kept a note of move attempts and the result of this check to see if the file was actually moved.
I even added a specific if statement to the Process.Start function checking specifically that a certain file was indeed in the required location before launching the game.
Finally before launching all of the files in the folder where the mod files should now be are written to the log.
All of the logs from users who the launcher didn't work for shared one thing in common the program attempted to move the file, threw no error, and then when checking that the file was indeed moved, the file appeared to be there. But when reaching the point where all of the files in the required directory are written to the log, only one of the required 30+ mod files appear to be in the directory.
An example output log looked something like this:
File moving: modfile1.txt
Checking if file is where we tried to move it to: Yes
File moving: modfile2.txt
Checking if file is where we tried to move it to: Yes
File moving: modfile3.txt
Checking if file is where we tried to move it to: Yes
Writing out all files in directory:
normalgamefile1.txt
normalgamefile2.txt
normalgamefile3.txt
modfile1.txt
After seeing this and noticing it was always only a single file that had moved and no others, and also that the program did think the files were where they where supposed to be, the confusion really started to kick in.
This is the method that reads the file to ascertain what type of file it is:
private int GetPackType(string path)
{
FileStream fs = File.OpenRead(path);
BinaryReader reader = new BinaryReader(fs);
reader.ReadChars(4);
int packType = reader.ReadInt32();
fs.Close();
fs.Dispose();
return packType;
}
As you can probably see, and what I've just noticed is that I've failed to close/dispose of reader, and I'm guessing and somewhat hoping this might be the cause of my problem.
(Note I'm now using Using statements in this method but can't test this fix for quite a while)
SO, if you've read this far thank you, my question is..
Firstly do you have any idea what the problem is? Could it be that reader still might have the file open and has not closed and thus the file can't move, IF SO then why does it work perfectly for me and most others, but not for some? Surely a file still being used elsewhere would throw an error?
I've gone through all the simple stuff like making sure the program is run with administrator privileges, etc.
Thank you greatly for any help!
EDIT
As asked in the comments this is the version of the code with my checks and logging added. Simple stuff, which is why I omitted it, the log simply adds to a string, the string is then printed to file when all is done.
private void Launch()
{
using (StreamWriter writer = new StreamWriter(userScriptPath, false, Encoding.Unicode))
{
foreach (string file in Directory.GetFiles(modFolderPath + "\\Files\\", "*.pack"))
{
int p = GetPackType(file);
if (p == 3)
{
if (File.Exists(modFolderPath + "\\Files\\" + Path.GetFileName(file)))
{
File.Move(modFolderPath + "\\Files\\" + Path.GetFileName(file), dataPath + "\\" + Path.GetFileName(file));
}
log += "Move File: " + Path.GetFileName(file) + "\r\n";
writer.WriteLine("mod \"" + Path.GetFileName(file) + "\";");
log += "Write mod line: " + Path.GetFileName(file) + "\r\n";
}
else if (p == 4)
{
if (File.Exists(modFolderPath + "\\Files\\" + Path.GetFileName(file)))
{
File.Move(modFolderPath + "\\Files\\" + Path.GetFileName(file), dataPath + "\\" + Path.GetFileName(file));
}
log += "Move File: " + Path.GetFileName(file) + "\r\n";
}
// Check the file did actually move
if (File.Exists(dataPath + "\\" + Path.GetFileName(file)) == false)
{
MessageBox.Show("The mod could not launch successfully!\n\nError: Packs failed to move", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
log += "File Found\r\n";
}
}
}
if (File.Exists(dataPath + "\\_GreatWar5_DB.pack")) // This pack should always be there if things have worked
{
Process game = Process.Start(gamePath);
}
else
{
MessageBox.Show("The mod could not launch successfully!\n\nError: Data pack missing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// There is code here waits for a minute until the game process is actually found, incase its slow starting up
game.WaitForExit();
RestorePacks(); // <- Method to put back the files after finishing
}
I am populating a treeview with a lot of data, and usually it takes a while for program to give back the control to user (I guess it is just too much process on the treeview side). So I decided to give a progress bar to show things are going fine to user.
In this treeview I select a folder and all xml files in that folder will be added to this folder and the treeview looks like this:
+ FOLDER NAME 1
|__xml file name 1
|__xml file name 2
+FOLDER NAME 2
|__xml file name 1
|__xml file name 2
So problem is, my progressbar will be completed actually much faster than the treeview itself, I think it is the time for progam that is making the treeview visible that takes the must time. How can I do this?
This is my code for browing a folder and passing it to a function to populate treeview (Sorry taht is too long but I though it gonna make it more clear) As you see I want that 50% of progress bar to be filled in this part and other 50 while adding nodes to treeview:
private void toolStripBrowseDirectory_Click(object sender, EventArgs e)
{
//Reset progressbar
toolStripProgressBar.Value = 0;
folderBrowser.ShowNewFolderButton = false;
DialogResult result = folderBrowser.ShowDialog();
int countAllfiles = 0;
int countGoodFiles = 0;
if (result == DialogResult.OK)
{
toolStripProgressBar.Value = 0;
string selectedFolder = folderBrowser.SelectedPath;
string[] files = Directory.GetFiles(selectedFolder, "*.xml", SearchOption.AllDirectories);
List<string> goodFiles = new List<string>();
toolStripProgressBar.Maximum = files.Length;
countAllfiles = files.GetLength(0);
//Folder name
FileInfo folderInfo = new FileInfo(selectedFolder);
string folderName = folderInfo.Name;
if (countAllfiles != 0)
{
foreach (string file in files)
{
//file name
FileInfo fileInfo = new FileInfo(file);
string fileName = fileInfo.Name;
try
{
XmlDocument checkFileType = new XmlDocument();
checkFileType.Load(file);
if (checkFileType.SelectSingleNode("//Document/#Type").Value == "Test Results")
{
countGoodFiles++;
goodFiles.Add(file);
}
}
catch (Exception a)
{
StartForm.setDebug("Comparison Mode", a.Message, 21, "Can not read Selected XML file '" + fileName + "'", "Check file for compatibilyty", "File was excluded from list");
}
this.toolStripProgressBar.PerformStep();
}
if (countGoodFiles == 0)
{
MessageBox.Show("There are no compatible XML files (exported from ZTR files) in '" + folderName + "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
StartForm.setDebug("Comparison Mode", "N/A", 20, "There are no compatible XML files (exported from ZTR files) in '" + folderName + "'", "Select a folder that contains compatible files!", "No file has been added to Select File Tree");
toolStripProgressBar.Value = 100;
}
else if (countGoodFiles > 0)
{
this.Cursor = Cursors.WaitCursor;
AddFilesToTree(folderName, goodFiles.ToArray());
this.Cursor = Cursors.Default;
}
}
else
{
MessageBox.Show("There are no XML files in '" + folderName + "'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
And this is for poulating the treeview
private void AddFilesToTree(string folder, string[] files)
{
//Define folder node
TreeNode folderNode = new TreeNode();
folderNode.Text = folder;
folderNode.Name = "folder";
folderNode.Tag = folder;
treeViewBrowsedFiles.Nodes.Add(folderNode);
//Define file node
foreach (string file in files)
{
try
{
//To check if the file is a valid test result (converted to XML from ZTR files)
XmlDocument checkFileType = new XmlDocument();
checkFileType.Load(file);
LoadZTRCmp xml = new LoadZTRCmp(file);
TreeNode fileNode = new TreeNode();
fileNode.Text = xml.FileInfo("BatchNumber");
fileNode.Name = "file";
fileNode.Tag = file;
folderNode.Nodes.Add(fileNode);
this.toolStripProgressBar.PerformStep();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
folderNode.Toggle();
}
So how to do this right way?
Update:
** SOLVED **
I found the problem, I had to do this line before using function to add nodes to treeview!!!
toolStripProgressBar.Maximum += countGoodFiles;
AddFilesToTree(folderName, goodFiles.ToArray());
fixed MAX value for progress bar
Still progressbar finishes (maxed out) before TreeView starts to load...
int progressSteps = (files.Length / 100)/2;
That's quite mysterious but I certainly don't understand the logic. The code is hard to read, you should break it up and use more helper methods. Try this instead:
toolStripProgressBar.Maximum = files.Length;
You can skip the progress bar if you just call Application.DoEvents() every so often, it'll show the TreeView expanding with the nodes and should be relevant enough to the user to know how much longer they have to wait.
Maybe use the progress bar mode of marquee, which just moves back and forth across the progress bar, and then hide the progress bar when the tree is done loading? That way you'd still get the visibility of the application is doing some work, and the user could know to wait?
progressBar1.Style = ProgressBarStyle.Marquee;
Check the value toolStripProgressBar.Maximum . Is it equal to the ProgressBar's Value when your process completes?
If your ProgressBar's Step property is 1 it should be equal to the number of steps you want to display on your ProgressBar. It's possible toolStripProgressBar.Maximum is set too high and so it never reaches the end before your process completes.
A better approach is probably to populate the branches of the tree on a need basis; so when your user opens a new branch, just get the sub-branches. Populating an entire tree in one go on start up is wasteful because the chances are that your user is only interested in a small part of the tree and does not want to wait for the entire thing to load just so that they can view the small part of the tree in which they are interested.
Take a look at the BackgroundWorker. It allows you to populate your treeview from a separate tread, making your user interface a lot more responsive. It also supports reporting progress, so if you still want to display a progress bar it is perfectly possible.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx