|
Maybe try a newer version (3.9):
Downloads
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
hai
am cyrrently working on a file monitoring system in c# that records all events in log format, with a command to dump logs as .txt files.
my question is, is the a code i could use to dump these log files into a file, save them at a specific time in the day.
say while its monitoring, it automatically saves the logs in a file at 23:59 everyday.
while monitoring a particular folder.
given there is a path in which to save.
|
|
|
|
|
|
You can either use a Timer, or check the time whenever you write a log record. Alternatively use a separate application the gets scheduled to run at a fixed time every day.
|
|
|
|
|
Why aren't you just writing the events to the log? If you're keeping this stuff in memory and writing it to a log later, you run the risk of running out of memory before you write the log!
|
|
|
|
|
We need to bring a Word document into focus and then paste text into the document at the current cursor location. We know the name of the Word document. Unfortunately Word only has one instance and one main window, which means that I cannot simply locate the window, bring it into focusand paste the data. Is there a way to do this from C#?
|
|
|
|
|
I hate applications that steal focus, as many other users do.
Do you need to show the Word-application, or would it suffice if you can embed Word in your application and push that to the foreground?
Inserting text into a document would be rather simple, as a word-document is a zipped collection of files you can read with notepad.
Do you want to replace text at a certain pre-determined position, or at the cursor? If the latter, you may be better of writing a Word-adding that doesn't steal focus.
Many questions here
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks for the response. Our requirements are that the user must be able to configure our system such that data from a reader is pasted into a user chosen application. Because this is generic, the way we do it is to bring the app into focus and then paste the data. For images we use the clipboard.
This is not intended to run on someone's work PC, rather it is an automated system, and giving an app focus is desirable as the operator can see the read data.
We wish to post into the cursor position in the target document.
|
|
|
|
|
In that case you would want to get a list of processes and enumerate its windows. You'd probably need some WinAPI to identify the correct window.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I already do that. Unfortunately Word only has one main window. I've tried to enumerate the child windows but that does not work, it only locates the topmost. If you run up two documents, then look in Task Manager, you'll see what I mean: one process and one window.
|
|
|
|
|
That's the only way I know to get the Windows that are created by the process.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
[EDIT]
I agree with Eddy Vluggen that you don't need to keep focus to the document/application to be able to paste piece od text. You need to refer to the document you want to use:
document = InstanceOfWord.Documents["ShortDocumentName.docx"];
document.Selection.PasteSpecial();
document = InstanceOfWord.Documents("ShortDocumentName.docx")
document.Selection.PasteSpecial()
More at MSDN: How to: Programmatically Insert Text into Word Documents
But, if a requirement is to keep focus, there's good news: there's buit-in method called: Document.Activate Method (Word)
InstanceOfWord.Documents["ShortDocumentName.docx"].Activate;
InstanceOfWord.Documents("ShortDocumentName.docx").Activate
|
|
|
|
|
I used to unit test private methods, then some people advised me not to do so...
What do you think? should I unit test only public methods or private methods as well?
|
|
|
|
|
This is one of those questions that can end up in semi-religious wars with people wanting to burn the heretics who don't do things "their way". If you are part of a team and the standard is to just test public implementations, then you just test public implementations. If that team insists on unit testing everything, then test the private methods as well.
I have to admit that I prefer tests that only test the public methods. Basically, if you have a private method that you can't get to somehow via your public methods then either that code is "dead code", or you have a serious problem with your architecture and are relying on reflection in the main codebase to get at this code and run it. Thing is, if you follow SOLID principals, no single part of your codebase should be that large that you can't adequately cover the private methods via the public methods/properties.
This space for rent
|
|
|
|
|
Very pragmatic! Spot on.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Don't you think that testing only the public methods creates a problem?
When a test of a public method fails, it is not clear where it fails. Is it in the public method called directly by the test, or in a private method called by the public method?
|
|
|
|
|
Member 13624012 wrote: Don't you think that testing only the public methods creates a problem? No I don't.Member 13624012 wrote: When a test of a public method fails, it is not clear where it fails. Is it in the public method called directly by the test, or in a private method called by the public method? Where it occurs is immaterial; the important thing is that the test failed. You have a predictable input (or set of inputs) going into a method that cause it to fail; debugging through the failure is a simple enough task that it doesn't matter.
To put things into perspective; if you have to debug private methods, you are introducing a coupling in that your test has to know how the methods behave and recreate the conditions necessary immediately before calling the method, so you need to take things like state into account. What you have done here is make it harder to change the working of code because any change will probably necessitate changing the tests as well. This is testing the "how" of the code. If you are testing public methods, you are more likely to be testing the behaviour of the code, so the inner logic has much more room to change.
An example might suffice here. Suppose I have a piece of code that writes to a log and returns a value to indicate that the log was successfully updated, and I have a test to ensure I get that value back; now, if I just test the public call of WriteToLog, I can ignore what happens inside and check the return value to ensure I get the expected one back - I'm ignoring the fact that, initially, I was logging to the file system (the how) and am now logging to a database. As long as I get that value back, I know that the particular piece of code behaved itself. This becomes much more important when you consider that I build up systems organically, so my initial internals would probably be a dummy logger which I would then build out and replace.
This space for rent
|
|
|
|
|
I would say that you have to test both, sorry.
Just to prevent you from hiding complexity in the private members and having each public member point to something private.
Bad of trust? Yes, but also a fan of proving that stuff works as intended. Too much proof is never a negative, is it?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Yes, I agree with you. I think that there's no such thing as too many unit tests!
|
|
|
|
|
The opinions vary on this subject, so I'll say it depends.
Those who oppose testing private methods, claim that you should only test the public API.
This makes a certain sense, as testing private methods will usually lead to testing implementation, while unit tests should test behavior and if that code needs to be tested then there's a code smell and a design problem. Also by testing public API you indirectly already test the private method - that's also what encapsulation is for.
The private testing proponents claim that you should test as much logic as possible and if there's a reason to test a private method you should.
I believe that good design would usually reduce the need for testing private methods, but there are still some reasons for testing private methods.
Let's not forget that there's also a lot of legacy code that wasn't designed to be testable, so testing private methods would make more sense in order to refactor that code.
Problem with that is how to test private methods, as mocking is usually limited to public and virtual methods unless you use "unlimited" mocking tools.
So you should decide what is the right path for you or your team, just be consistent.
|
|
|
|
|
It depends on your risk-estimation.
If you think the private methods are fault-free. => No UnitTest
If you think the private methods can have bugs, but the consequences are tolerable. => No UnitTest
If you think the private methods can have bugs, but the consequences are NOT tolerable. => Unittest them.
Example 1:
If you are contributing "private" code to your team and you think some bugs are tolerable => No Unittest.
Example 2:
If your "private" code controls the cooling-system of a nuclear power-plant and you think the risk of bugs is tolerable => No UnitTest
If you consider your question in this way (as I do in practice), the answer is quite easy.
If you still don't know what to do, ask your boss, if he accepts bugs in "private" code or not.
modified 23-Feb-18 16:34pm.
|
|
|
|
|
Hello:
I simply want to write multiple queries to assign information.
var q =
from a1 in dt_Jobs.AsEnumerable()
orderby a1.Field<String>("Customer")
select a1.Field<String>("Customer");
var lstCustomer = q.Distinct().ToList();
cboCustomer.DataSource = lstCustomer;
q =
from a1 in dt_Jobs.AsEnumerable()
orderby a1.Field<String>("City")
select a1.Field<String>("City");
var lstCity = q.Distinct().ToList();
cboCity.DataSource = lstCity;
The first block works, the second fails. How can I achieve something like this??
Thanks!
|
|
|
|
|
You're going to have to explain what "fails" means. Did it throw an exception? What was the message?
Does something not work as expected? How so and under what conditions?
|
|
|
|
|
I am trying to set the datasources for comboboxes.
There is a datasource in the first, but not in the second. If I flip them around, I get the opposite effect.
|
|
|
|
|
Sorry for the confusion, there are errors.
Exception thrown: 'System.FormatException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in ResourcePlanningC2.exe
Exception thrown: 'System.InvalidCastException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in ResourcePlanningC2.exe
Exception thrown: 'System.FormatException' in mscorlib.dll
Exception thrown: 'System.NullReferenceException' in ResourcePlanningC2.exe
Exception thrown: 'System.NullReferenceException' in ResourcePlanningC2.exe
Exception thrown: 'System.FormatException' in mscorlib.dll
The program '[12772] ResourcePlanningC2.exe' has exited with code -1 (0xffffffff).
It must be hanging on some null values. Is there an easy way to handle this?
|
|
|
|