In my project I need to watch multiple FTP Folders continuosly, if any new file comes i need pick the filename and do some process. If that is normal Windows/Local File System folder I can achieve this by using SystemFileWatcher of .net. Is there any thing like SystemFilewatcher to watch FTP folders?
nrk
No this doesn't exist, because a FTP folder can't send you any events about any change. So you have to write your own little class with a background worker. This one asks the ftp server for directory listing periodically and compares it against the last list obtained. Then you can fire some events depending on the happen changes.
So you'll get an event class in .Net but under the hood it will be pull model with lot of traffic on the wire.
I don't understand #Oliver's answer. Of course there is a way to use the FileSystemWatcher class to watch events in an FTP folder, and it's not a pull model. I'm working on creating something just like it right now, using the model as described in this MSDN article:
Windows Services: New Base Classes in .NET Make Writing a Windows Service Easy
To effectively use the FSW, you build it into a Windows Service, and have it watch the FTP folder(s). Of course, you'd need to install it on the FTP server (not somewhere else in the network), but it would do exactly what you need.
Related
I have an asp.net page that allows a user to select an excel file on his computer and then (1) upload it to the server, and (2) import into a SQL server table. This works fine.
Now I want to enhance the pages functionality to allow the user to select a directory on his computer and then automatically upload/import EACH file (Only of types xls, xlsx) in the clients directory. How can I do this? (Alternatively the user can select a file and then check a checkbox that says "upload all files in this directory", then it will process each file, etc.)
I found a way to loop thru the directory on the server side. But I can't figure it out for client side. Thanks!
You can't. Or at least, you shouldn't be able to.
A properly written browser specifically prevents direct access to the file system of the computer it is running on as a security precaution. The only "correct" way to access a file on a remote machine is to use the browser's File Upload form control on the page and have the user specify the file (or files) they want to send you.
Basically, no.
Many advances have been made in browser security to stop people doing this sort of thing.
The main solution is to use some kind of local plugin that gives this sort of access, but as this can be used for many nefarious deeds, it's very much frowned upon now.
You can of course let the user choose many files, there are various methods for doing this - but you can't automate it by the browser.
What's to stop someone searching for 'creditscards.txt' and uploading it without the user being aware?
One way I can think of is a Java applet. This is executed locally and has full access to the computer and the browser. They are being shut down for similar reason: it's just not safe.
I have outlined a way, but I must emphasize that this must be avoided. It's not because you can that you should.
I was wondering from a programmers perspective preferably in C# or Java, how hard is it to manipulate a folder within the operating system to do what you want, for instance to act like an FTP?
image http://www.jacks-log.de/wp-content/uploads/2011/02/Dropbox-Wurmloch-1.png
Since I heard of dropbox a few years ago, I've always been wondering whats involved in doing this?
Talking about Dropbox in particular, they don't do anything tricky - just a regular folder being monitored using the mechanisms provided by Windows user-mode API.
It is possible to do much more powerful and interesting things using kernel-mode drivers, though.
First thing you can do is create a virtual disk and map it to the folder. This way your code (either kernel-mode or user-mode with help of our products) will be able to handle all OS requests coming for files in this folder. The folder won't exist on the disk but will be virtual. You can pick data from the remote server on the fly, write them to the remote server etc. In particular, you can map a remote SFTP server to the folder (with FTP things are a bit more complicated as FTP doesn't support partial uploads). Some cloud providers do exactly what I described - they offer a virtual disk or a virtual folder which is a "gate" to their cloud service, and when you read or write data from such virtual folder or disk, data goes from/to the cloud server.
Next thing is that with help of the filesystem filter driver you can have a real folder but provide file contents dynamically. This is how some folder encryptors do (they encrypt file data when writing it to the real disk and without this encryptor active you will read just encrypted "junk" from files in this folder).
Now, with filesystem filter driver you can control who and how can access the folder, i.e. you get fine-grain control over access to folder contents (combine this with encryption and you get a handy data protection mechanism).
I want to develop the application which logs the files / directories accessed on the machine.
e.g. I go to D:\ and into a folder documents and open a word file. I want my application to create a log in the following format:
D:\ Opened
D:\documents Opened
D:\documents\secret.docx Opened
I've used FileSystemWatcher to achieve the other type of file system activity but unable to get events for accessing this.
sounds like you wanna do a FileMon program like sys internals. in their website Mark tells about the way FileMon works so you can get some inspiration by reading the article.
also see here: How do you monitor file access and changes on a file server by user name?
Not sure this sort of monitoring can be achieved with filesystemwatcher as it is aimed at monitoring changes I believe. You could use filesystem Auditing (by going into advanced security settings) which will log events in eventlog and you can pull it from there.
Most viable option is use of file system filter driver. Such driver gives you fine-grain control over all requests going to particular file system. The only issue with this approach is complexity of developing such driver in kernel mode.
In my application, I download files in network drive to local and then process them.
Now my problem is network path changed from windows to ftp and need user id password to access that.
My application code is in C# and which can not accomodate this change directly,
how do I take care of this within code -- with minimal effort.
Thanks
The easiest way to use ftp in your app is to get one of the many ftp client packages. At work we use the Xceed ftp client and it seems to work fine, although it is a little pricey. Here is an open source project I found by googling for .NET FTP Client:
http://www.codeproject.com/KB/IP/FtpClient.aspx
Add an interface (say IFileHandler) which defines the contract for the getting of the remote file, then implement what you currently have, to get from a network drive, behind one implementation of that interface (say called NetworkFileHandler).
Then create a second implementation (say FtpFileHandler) of that interface that gets the file from FTP site. In the second implementation you give the extra information (username and password) to the constructor of the FtpFileHandler class.
Then to change the app to do one or the other you just need to change which implementation of the interface you use.
This would then allow you to add other ways of getting the file in the future like BitTorrentFileHandler for getting files from a bittorrent source or NntpFileHandler for downloading the files from a newsgroup.
The interface IFileHandler could look something like:
interface IFileHandler
{
void SaveFileLocally(string sourceFile, DirectoryInfo targetLocation);
}
although exactly what it should look like will depend on what you need to do.
I need to Read files form Linux , copy them into the another computer(Windows operation) and even delete file in Linux. but I want do this with .net program.!!!
these file have a specific location.
I need code sample or article for doing that.
As stated before, there are several ways to do this:
Set up a samba share on the linux box, that lets you access the files from your windows box with your .NET programm. Beware of not to expose private files to the net (like /etc or /var). Additionaly do not expose actively used files since it will cause unexpected behaviour if your deamon tries to access a file, that your programm works on. Just access exported, passive data files over the net.
Use libraries for SSH with your programm to access the files on the linux box programmatically.
Set up a cron job on the linux box, that copies the files regularly to a share on your windows box to be processed over there.
Set up a deamon in mono/.NET that runs on the linux box and passes the files over an API to your windows client .NET app.
Write the whole programm in mono/.NET and let it compute on the linux box.
Just some approaches to a solution, just pick one that suits you.
There are plenty of ways to do this, most of which have nothing to do with writing code. The most obvious is to share the Linux files with the Windows machine using Samba.
Use something that speaks a useful file transfer protocol.