FTP Several Files - c#

I have what would seem like a common problem, but I cannot find an appropriate solution on any forums. I need to FTP an entire directory structure using .NET. I have found several code examples all of which show how you can FTP a single file by creating an FtpWebRequest object. Unfortunately, there is no information on how to deal with several files. Do I simply create a FtpWebRequest object for every single file?

You can always call a new Process with a command like using for example WinSCP (open source FTP client)
https://winscp.net/eng/docs/start
Perhaps call the synchronize operation:
https://winscp.net/eng/docs/scriptcommand_synchronize

If there's a shorter way, I don't know what it is.
I wrote my code to handle each file one by one. If you are dealing with entire directory structures, that would entail processing each directory one by one as well.

Related

Transactional Write of multiple FileStreams in C#

I'm supposed to get multiple files with the same extension (for example, an archive that's split into several archives: rar, r00, r01, etc).
I'm trying to find a solution where if one of the file streams I got fails to be written, all the previously successful files that were created will be deleted.
Just like a transactional file stream writer.
I've bumped into .NET Transactional File Manager project, which seems just like what I need -- except it doesn't work with streams but with file paths.
At this point I only see two options:
Keeping a list of successful file writes and if other one will fail, I'll go over the list and delete all.
Write all files to %TEMP% or something via FileStream and then - after all files were written successfully, I'll use the Transactional File Manager (mentioned above) to move the files to the desired location.
Need to notice that I have to work with streams
Which of the two options is better in your opinion?
Is there any better recommendation or idea for doing this?
Thanks
Edit:
Another option I bumped into is using AlphaFS just like in the following example.
Any thoughts on this?
I ended up using AlphaFS.
Using it just like in this example.
It works perfectly and does exactly what I was needed.
Thanks for all the comments.

Transactional file management in C#

I have a scenario in my application is that i need to upload some files (Zip files) from the client to the server and in the server i want to extract the Zip file and replace those files which i getting from extracting the Zip file into some other folder.
The files which i need to be replaced is mostly dll files. So one thing that i need to ensure that either all files should be replaced or none of them get replaced.
Is there any way in C# to achieve this (like Transaction in SQL) ? If anything bad occurs while replacing files (Example: no memory space), every changes happened to the previous files should be rollbacked.
Hope you understand the problem.
Any help ?
NTFS allows file system transactions, see https://msdn.microsoft.com/en-us/magazine/cc163388.aspx
Having a quick poke around, only way I can see you doing this would be through https://msdn.microsoft.com/en-us/magazine/cc163388.aspx which involves some native code. Otherwise you could use a third party tool such as http://transactionalfilemgr.codeplex.com/
If you wanted to manage it yourself or go for a simpler approach, I would suggest backing up the existing files somewhere before trying to copy the new files. This could be in another folder or zipped up. Then if the copy fails, you handle this and revert all the files to their original state.
Whatever you choose, make sure you have plenty of logging so you can see what's happening and if/when something goes wrong :)

Update file, not replace or overwrite

this is more of a question because I am experimenting with this.
All over the internet I see how you can update a .txt file. Well that is all good and well, but lets say I have a .doxc or even an .exe or even a .dll file.
If we make a minor change to a file, do we really have to replace(overwrite) the whole file?
Is it possible to "update" the file so that we don't use too mush data (over the internet).
What I am trying to achieve is to create a FTP client with a FileSystemWatcher. This will monitor a certain folder on the Computer. If anything changes in this folder (even sub directories) then it uploads, deletes, renames, or changes the file. But at the moment I am wondering if I have, lets say, a 20MB .exe file or whatever, if it is possible to change something in that .exe, instead of just overwriting the whole thing... thus, sparing some cap.
In general, it's possible to update the remote file only partially, but not in your case.
What would work:
1) track the file change using a filesystem filter driver, which gives you information about what parts of the file have been updated.
2) use the protocol which allows partial upload or remote modification of the file (eg. SFTP).
As for your scenario:
Step 1 is not possible with FileSystemWatcher.
Step 2 is not possible with FTP protocol which doesn't support modification of file blocks.
Since your are experimenting, I can provide some pointers. But I dont know for sure if the below operations are just updates or replaced newly by the underlysing os calls
Have different cases for each file type. Try with a basic types first, a txt file, then a binary file etc.
You should have the entire copy of the current file somewhere, sine you "should" compare the old file to know what changes
Then when a change is made to the file compare it with the old file e.g) in a text file with 1 MB and the change is only 1 KB you will need to build a format like
[Text][Offset][[operation]
e.g) [Mrs.Y][40][Delete] then [Mr.X][40][Add]
Then your ftp client should be able to implement this format and make changes to the local copy on the client.
No it is not possible to only upload the changes to .exe file.we have to overwrite it.
#Frederik - It would be possible if FTP supports an updating of resource like HTTP's PUT command. Try exploring that angle. Let us know if you find something.

Folder explorer options

I have recently been assigned a task which sounded relatively simple!
Upon attempting it became clear it wasn't as straight forward as i first imagined!!!
I am trying to download multiple files to one location on the users machine. They select these files from lists within a custom share-point web part. Thats the bit i have managed to get working! The downloading is done via WebClient (System.Net.WebClient)
I now want to allow the user to select a location on their local machine to download the files to.
I thought i would be able to use but after attempting this i realized i can only pick files :( in order to get the desired location which will confuse the user
I want something similar to the above but i only need it to return a path location like c:\Temp or any other location the user prefers on their local machine.
Could anyone suggest a control that could provide this functionality. It can also be a share-point control.
In the meantime I will be attempting Tree view as i have never used these before and these may have the power to do this from what i have read
Cheers
Truez
Clarity on language ASP.NET
Unfortunately, you can't do this without some kind of active content, like a Flash control or spit activeX /spit.
It seems strange at first, but you have to consider that this kind of functionality would let a site discover the structure of anyones storage devices; this is not 'a good thing'™
However, perhaps a different approach might solve the problem?
Why are you using WebClient, can't you provide the link to the client and let them choose their own download folder ?
I ended up zipping the files in to one folder and passed the file to be downloaded through the browser! Thanks for your comments!

How to send folders in c# through TCP?

I'm having trouble looking for a way to send whole folders over TCP. My initial idea was that the sender sends a string that contains the path of a given file like C://MyFolder/MySubFolder/MyFile then the receiver creates the folders and subfolders. The sender then goes ahead with the sending of the files containing their directory.
I think it goes without saying that this is not the best method in doing this. Is there a better approach?
EDIT:
Sorry if I was a little vague. I have a file transfer app that sends sends/receives files obviously and I want to add a way to send whole folders.
You need some sort of a file transfer protocol for that (i.e. FTP). Use an easy to setup c# FTP server library (i.e. this one: http://sourceforge.net/projects/csftpserver/) on the sending side and use FtpWebRequest on the client side to get the whole folder structure.
Use famous archiving methods (zip, rar...) and transfer data. The extract the at the peer side. This way you save:
Implementing an error-prone
recursive pattern.
Your bandwidth
Have you looked at existing protocols for this purpose? It seems you want to clone FTP, maybe with a streaming mechanism like tar in between.
If you consider zipping/compressing:
You could have a look at GZipStream class for that.
http://www.geekpedia.com/tutorial190_Zipping-files-using-GZipStream.html

Categories

Resources