|
Given the following code:
FileSystemWatcher m_watcher = new FileSystemWatcher();
private void MyMethod()
{
m_watcher.BeginInit();
m_watcher.Filter = "*myfile*.*";
m_watcher.IncludeSubdirectories = false;
m_watcher.Path = "C:\\MyEwatchedFolder";
m_watcher.NotifyFilter = NotifyFilters.LastWrite;
m_watcher.Changed += new FileSystemEventHandler(watcher_Changed);
m_watcher.EndInit();
m_watcher.EnableRaisingEvents = true;
}
void watcher_Changed(object sender, FileSystemEventArgs e)
{
FileSystemWatcher watcher = sender as FileSystemWatcher;
if (e.ChangeType == WatcherChangeTypes.Changed)
{
MessageBox.Show(string.Format("File!\n\n{0}", e.FullPath));
}
}
The Changed handler is getting called twice. I've looked all over googkle and can't find a reason or a way to fix it.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Have to tried increasing the FSW's InternalBufferSize?? It must be a multiple of the O/S page size(?? I think!). Default is 8KB. Keep in mind that each event that is internally queued needs 16 + (2 * filename length) bytes to store, so if you're encountering long filenames, you could overflow the buffer and not know anything about it.
|
|
|
|
|
The file names are approximately 30 characters long, but I'm only throwing one at a time at the application as a test, so I don't think the buffer would be filling up, even at the default 8k size.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
|
AFAIK FileSystemWatcher makes only a best attempt at signaling file system actions; it does not guarantee to report everything, and I expect it to report more actions per file than you care to receive, e.g. writing a file changes the content (once or more) and changes the metadata (such as last write time); the file system itself only guarantees metadata once the file got closed, but AFAIK may or may not update things in between. So you really have to defend yourself against multiple reports.
|
|
|
|
|
I set up a list of recently "handled" filenames, and if the name is in the list, I don't do anything. If it's not in the list I add it. If the list grows beyond a certain threshold, I delete the first item in the list. It's a kludge but it appears to work fine since I'm not expecting more than a couple of files at a time.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
OK
FWIW you could turn it into an MRU list by:
first removing the new item, if that fails and list filled to capacity removeAt(0);
then always add the new item.
|
|
|
|
|
I've run into this also with the change event. As I recall it is accurate because file systems generates multiple actions when a file is changed. I was able to get around it with this bit of clugyness
void Watcher_Changed(object sender, FileSystemEventArgs e)<br />
{<br />
Watcher.EnableRaisingEvents = false;<br />
Watcher.Changed -= new FileSystemEventHandler(Watcher_Changed);<br />
<br />
<br />
Watcher.Changed += new FileSystemEventHandler(Watcher_Changed);<br />
Watcher.EnableRaisingEvents = true;<br />
}
only two letters away from being an asset
|
|
|
|
|
Hello,
This is normal, what you see as one logical operation actually involves two different physical operations that throw this event. I've learnt that a few years ago, I just can't remember what were the physical operations exactly but it made sense. Something already occurs when the file gets opened or when the write operation is not yet flushed, but the fact is that you can actually detect that. A the first event, the file size is not yet correct: in the case of a new file, the size will be zero, and for modifications... well you have to know the previous size
Below is a sample of something I wrote a few years ago, please forgive me I wrote it in VB and I am not particularly proud of this piece of code Here, I had the advantage that I was monitoring one file, so I kept remembering its size.
I remember there is a more elegant solution in the source code of Log4Net, the popular open source logging framework. They use a file system watcher to watch their own config file for changes. If I remember well, they make use of timer or something like that, the technique works always and you always get only 1 event.
Sorry it is late for me, but you should look at this source code, it contains your answer
JC
Private _ChangeWatcher As FileSystemWatcher
Private Sub InitWatcher()
_ChangeWatcher = New System.IO.FileSystemWatcher()
AddHandler _ChangeWatcher.Changed, AddressOf Watcher_Changed
With _ChangeWatcher
.BeginInit()
.EnableRaisingEvents = True
.IncludeSubdirectories = False
.NotifyFilter = IO.NotifyFilters.LastWrite
.Path = _FolderToMonitor
.Filter = _FileName
.EndInit()
End With
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' Called when the FileSystemWatcher detects a change (matching
''' the NotifyFilter) on the specified file. In this case, the handler
''' will be called when a file is created or updated.
''' </summary>
''' <remarks>
''' We cannot rely on the ChangeType, it is always CHANGED,
''' even when the file was created. Luckily, it has no impact.
''' </remarks>
''' -----------------------------------------------------------------------------
Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
SyncLock Me
If String.Compare(e.Name, _FileName, True, CultureInfo.InvariantCulture).Equals(0) Then
If e.ChangeType.Equals(WatcherChangeTypes.Changed) OrElse e.ChangeType.Equals(WatcherChangeTypes.Created) Then
Dim _FileInfo As New FileInfo(e.FullPath)
If _FileInfo.Exists Then
' Avoid multiple notifications for the same event.
If Not _FileInfo.Length.Equals(_PreviousFileSize) Then
_PreviousFileSize = _FileInfo.Length
InitiateLoad()
End If
End If
End If
End If
End SyncLock
End Sub
Jean-Christophe Grégoire
|
|
|
|
|
Imagine a class with the following properties
Shape //differnt shapes
Colour //different colours
Fill //yes or no
Border //yes or no
Etc etc – there could lots of different ones, maybe even a 100
Each value associated with the above properties has to have a key/value pair (eg [1] [Red]), the number will be used to submit to a 3rd party software and the text to give the option to the user.
Now how should I store them?
I could use a hashtable to store the options as DictonaryPairs, and then the data type of each property could be a DictonaryPair. This works in theory, but in practice it means the fill could be given [1] [Red] instead on [0] [No]
So I then thought I could inherit from DictonaryPair to make
ShapeDictonaryPair
ColourDictonaryPair
BoolDictonaryPair
And then the data types could be the following
ShapeDictonaryPair Shape
ColourDictonaryPair Colour
BoolDictonaryPair Fill
BoolDictonaryPair Border
Which would work, but I will end up with loads of different Pair classes, which I think I will have to.
So then, would you use Hashtables or generic lists? The items in the list or hashtable will need to end up in combo boxes, with the user seeing the value and then store the pair
Or any other ideas
Did that make sense
|
|
|
|
|
You don't really need to create that many pairs - use the generic KeyValuePair and Dictionary classes instead.
like
Dictionary <int, Color > dict = new Dictionary <int, Color >
dict.Add(1, Color.Red);
dict.Add(2, Fill.All);
|
|
|
|
|
Looks good, but it wont work for my needs (my fault for not being detailed enough in the first post, sorry)
The value must be a string, so they will all be
Dictionary <int, string >
The 1st post was only trying to give an example, and in the real app, the values will be things that are not represented by .net classes (I could make classes for each type, but again that means loads of classes)
|
|
|
|
|
I really have searched for this solution online and haven't found it. I want to do be able to get the increment value that the access DB has created. Now I know I should be using a scalar query and I should be using something called @@identity. How it will all come together in my code is what I really can't seem to figure out.
|
|
|
|
|
Simply try
SELECT @@IDENTITY
with a SqlCommand and ExecuteScalar method.
|
|
|
|
|
will it be "SELECT @@identity FROM tableName"?
|
|
|
|
|
|
SELECT @@identity
This is typically done at the end of an INSERT to the database, where the database returns the identity of the new row, normally from a stored procedure. You cannot use it any time you want in an attempt to get the id of the last record. The concept just doesn't work that way. For example, what if you had two clients execute that statement on the database at the same time?
You can read up more on it here[^]. Keep in mind, that you're going to have to also read up on what an SQL Session and Scope are to fully understand the material.
|
|
|
|
|
I am writing an application that will give me content of a web page.
I wrote following code for that.
WebRequest objRequest = HttpWebRequest.Create("url");
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
This works fine if the url i am connecting to does not require to login.
I want to read content of a web page which requires first to put in login details (user/password) to get access.
for that i tried with
WebRequest objRequest = HttpWebRequest.Create("url");
NetworkCredential netCred = new NetworkCredential("User","Password");
objRequest.Credentials = netCred;
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
No success with this as well. It gets the contents of log in page and not the one specified by url.
Can anyone help me with this?
How to Read contents of a webpage which requires to login first.
Thanks,
AksharRoop
|
|
|
|
|
AksharRoop wrote: I want to read content of a web page which requires first to put in login details
Do you log in through a web page, or does your browser display a login dialog for you?
If you use forms based login (i.e. through a web page) then you will have to find a way to replicate that as if the user was actually sitting at the browser.
|
|
|
|
|
I have already logged on to that site using web-browser.
I am writing an automated application that will read the content of web page and send it across.
for that i will be logging on to web-page through browser and at some particular time my application will be triggered to read and send data.
I tried with the code posted but it did not work for me.
I am using Mozilla browser. Is there any way to connect to the same session?
|
|
|
|
|
AksharRoop wrote: I have already logged on to that site using web-browser.
I would imagine then that it stores a cookie with the session information. You would have to replicate the contents of that cookie and send it with your request in your application in order to get to the password protected areas.
AksharRoop wrote: Is there any way to connect to the same session?
Not that I know of. You would have to replicate the session (probably using the above)
|
|
|
|
|
I am new to C#. Could you please put code snippet?
|
|
|
|
|
A recent thread[^] got me wondering, when is the goto command actually acceptable? I've always steered clear of it and it seems to be an easy one to stay away from. I think the last time I used goto must have been somewhere around 1984, using GWBasic so I've kind of forgotten of its existence.
But if it's part of the C# language it must be there for a reason. Could anyone give me examples of where you would be willing to use the goto statement?
|
|
|
|
|
I can think of three cases:
1. An error condition is discovered when you're in deeply-nested multiple loops, and you need to stop this processing and go on to something else. A goto will exit this cleanly.
2. In grad school I worked on a system that created programs automatically by wiring together mechanisms (task-specific source-code procedures). Goto statements were exactly what was needed for sequencing their invocation.
3. A compiler bug prevented the normal sequence of execution, and time was running out. A goto was used to force control to the next statement to be executed, solving the problem in time.
I haven't had to use a goto in over 15 years. They can easily make your program's flow more complex than it has to be. And complex programs are harder to debug, and less reliable. 99.99% of the time you can avoid gotos.
|
|
|
|
|
switch
{
case a:
break;
case b:
goto default;
case c:
break;
default:
break;
}
while (someConditionA)
{
while (someConditionB)
{
foreach (something in other)
{
if (exit condition)
goto Label_ExitLoops;
}
}
}
Label_ExitLoops:
Only two situations I ever use it for. Deeply nested loops could be exited with a series of flags, but I think the goto is a lot cleaner and more efficient.
|
|
|
|