C#/WPF: Implementing Autosave - c#

how could i implement autosave in C#? i felt that saving to the currently open file is a simple but i may not want to overwrite my previous file (or should i just do that? i think google docs saves/overwrite the document tho they have version control?). i thought of saving to another file, but where do i save to?
also i guess i will have to know if there's any autosave files to retrieve and after a explicit save, i should remove the autosave file associated with the current document

Current versions of Microsoft Office (for example) save to a "shadow copy" of the working file. Depending on how you want it to work, you can have writes applied to the shadow copy every n seconds or when certain types of actions are taken.
When the program is shut down, the original is deleted and the shadow copy is renamed to the original. There are lots of options and strategies within this technique that can be applied depending on your particular situation and requirements.

You should create a Temporary File for the autosave. If the User saves, you can delete the file, and if your app crashes and is restarted it can load the last autosaved state from the file.

Related

Use the standard "overwrite" dialog with custom information

I am writing a utility copying files to/from remote drives (eg SkyDrive). When I'm about to overwrite a file, I pop up a regular "file exists, do you want to overwrite Y/N" message box; is it possible to use the standard Windows file overwrite dialog instead? With the three options (copy and replace, don't copy, copy but keep both) and the more detailed size / date information? Or should I just write my own form to duplicate that? (To be honest, I've been searching for this for two days, it would probably have been much faster to just write my own.)
To clarify: I need to show up that dialog when I choose to - sometimes I don't want to show it and just want to overwrite the file.
If at all possible, I would prefer for this to work in C# / .NET; however, if only an unmanaged solution exists I'll take that too.
Yes, it is possible! I did this for Send to Dropbox a little add-on utility I wrote for Explorer to right click and send files to a dropbox folder.
Copy SHFileOperation.cs into your project, and you can invoke it like so:
ShellFileOperation.CopyItems(source, target);
source can have multiple files/directories. Each item must be enclosed in quotes (").
target is the destination directory.
Example:
ShellFileOperation.CopyItems("\"c:\\foo.txt\" \"c:\\bar.txt\"", "d:\\xyz");

Calculating and caching folder sizes

I have an idea for a C# program that works basically like the Windows Explorer. The aim is to display all files and folders, and to show specific information for each of them. One of the features I'm planning is to detect folder sizes which is something the Explorer cannot.
My idea for the algorithm is to cumulate the sizes of all files in the specific folder. However, I'm afraid of performance issues. For example, for displaying the sizes of all folders of C: I have to consider all the files on the whole drive. This will probably take a while and thus the calculation can't be done each time the user switches to a different folder or back.
So I'd like to cache some of the sizes. However, when files change, are added or removed, the cache data becomes outdated. But I do not want to monitor all file changes while the program is not running.
Is there any way I can find out if the cache is up-to-date, e.g. by retrieving some sort of checksum that doesn't require calculating all sizes again? Is there another memory and CPU-efficient way to find out if file sizes have changed since the last calculation? Or is there even another possibility?
Windows Explorer has the Folder size available (# files, size on disk etc) availble for the properties of any disk/folder. Directory Properties Example
As for writing a program, you can certainly use a recurisve DirectoryInfo.EnumerateFiles() to get all the files within a disk/folder.
As for monitoring, you can use the FileSystemWatcher class to monitor changes to any disk/folder.
To keep the cache up to date is going to be difficult because:
Depending on the Partition Formated Type [Fat, Fat32, NTFS, etc] you are limited to what each support.
Any new file (created date > cache date) means you still have to enumerate all the files to filter the list to new files.
Modified files (modified date > cache date) has the same issue.
Unless you use something VERY specific to the Formatted Type beyond what C# provides, updating a cache after the application launch will need to occur every time, and be very intense.
Windows Explorer is a pretty crafty program. It is filled with tricks that are designed to hide the fact that any file system is punishingly slow to iterate. The kind of tricks that I know about:
fake it. Show the folder hierarchy as a treeview and use the [+]
glyph to show that a folder has files or directories inside of it.
Even when it doesn't. That's visible, create an empty directory and
restart your machine. Note the [+] glyph, click it and notice that,
when forced to iterate the sub-directory, it smoothly change the [+]
glyph to a 'nothing there' glyph.
delay it. Harder to see, you need a subdirectory with a lot of
files. Explorer starts a background thread that iterates the
content of the folder. Once it figured it out, it smoothly changes
the status bar text.
tell me what happened. Explorer uses ReadDirectoryChangesW()
heavily. Wrapped in .NET by the FileSystemWatcher class. Key point
is that it gets a notification that something changed in the
subdirectory that the user is looking at. No polling required, that
would have horrible perf. Go back to bullet two.

Safe way to programmatically move a batch of file?

What does one need to take care of when creating a method to move (cut) a batch of file from one directory to another?
Let's say the method signature is Move(filter, sourceFolder, destinationFolder, overwrite). What do I need to take care of to avoid the risk of data loss especially when overwriting the original file and deleting of the source file is taken into account?
Several possible scenario I am worried of: error occurs when a move is in progress, moved a file but the file are somehow corrupted, deleted a namesake file in order to allow the new file to move but then error happens when moving the new file, etc.
I'm using .net's System.IO namespace for the move operations.
Without transactions, the safest way is to copy, verify and then delete. It is up to you if you want to move per file (this is how windows does it, a move operation can fail leaving you with half of the files moved) or to allow only the entire batch to be moved, or none at all.
You would have to make decisions on how to respond to files that have been modified during the move, source files that cannot be deleted afterwards, or destination files that have already been opened when you're performing a rollback.

Is there a way in c# to know when the user has finished editing a file with an application?

This is what I'm trying to do :
Download a file (txt, doc, xls, whatever) from a server
Open the file with the appropriate application using System.Diagnostics.Process.Start(path to file)
Monitor for file changes using a FileSystemWatcher.
Each time the file is changed, upload the file back to the server
Continue monitoring until the user has finished editing the file
Delete the local copy of the file
Exit the application
I'm stuck at step 5. How can I know whether a user has finished working on a file ?
I cannot rely on the file being locked (notepad doesn't lock txt files for example).
I cannot rely on a process having exited or not (an example is Notepad++ for txt files : the file could be open in a tab. When you close the tab, you've finished editing the file, but the process is still running)
Any idea/points on how to do that in C# ?
You've excluded the two ways you could go about detecting the file being in use: file locking, and the process you start exiting.
The only alternative I an think of is to display a dialog to ask the user when they've finished editing.
Edit: For what it's worth - FileZilla has this type of behaviour. You can choose to edit a file on the remote server, it downloads the file, launches the default editor, and (in the background) shows a "If you've finished editing - Click OK" button.
This gives me the opportunity to cancel an edit, if I've mucked up the file and saved it.
This is really hard to do - we've tried various things but never found anything that was foolproof. If you know the program you have launched then, in theory, you can find the file handles it uses and see when it stops using the one you're interested in.....but if you rely on Windows to resolve the default application to launch even this becomes tricky.
We copy editable files into a temp folder named with the date and rely on users uploading them back when they have finished their edit session. We then clean up previous days folders on application startup.
You could check the date of last change of the file. This date gets set when you save changes to the file. Mind though that this field is not very reliable since one can set it to any value (with appropriate tools).

How can you detect when files are dropped into Windows Explorer?

I have a WinForms app that has a TreeView. The user can drag files from WindowsExplorer to the TreeView, and then they can drag the files back into WindowsExplorer which in affect copies the files to wherever the files were dropped. What I'm trying to do is, if the files already exist in the directory where the files are being dropped, I want to rename the files/folders being copied in ahead of time, so that there's no collision.
Here's how I'm copying files into WindowsExplorer. On the treeView's ItemDrag, I loop through the nodes of the selected node, and then package that into an array. Then, I use this code:
var dataObject = new DataObject(DataFormats.FileDrop, files.ToArray());
dataObject.SetData(DataFormats.StringFormat, dataObject);
DoDragDrop(dataObject, DragDropEffects.Copy);
This works well, but once it ships off to Windows Explorer, it's out of my hands. How can I find out when and where the files are being copied TO and intercept that to make changes? Is this possible?
Explorer Drag & Drop is an excellent article doing what you are trying to achieve.
EDIT2: It seems that there's a C++ article available for the same on CodeProject. But I was unable to find a way of how to do it using C#.
AFAIK, there is no way to know drop target (in your case destination folder). You can look into CFSTR_FILENAMEMAP shell clipboard format, but still in this case you can only provide name mappings before (or in process) of drag-n-drop.
Also note, that default DataObject in .net has limited shell support. So if you need to use mentioned above format, you need to write your own IDataObject implementation (or take someone's implementation, good example with lot shell drag-n-drop related stuff can be found here)
Instead of putting the file names into the dataobject, create a temporary file with a unique/easily distinguishable name and place that file name into the data object's drop list instead (That file could be empty or contain some information you might need). Use a FileSystemWatcher (watching an entire drive) to detect the drop (set the filter to the temporary file name, set IncludeSubDirectories to true, and set Path to root directory of drive to watch.) Initiate the DoDragDrop. Once the unique/easily distinguishable file is dropped, the FileSystemWatcher can tell you where it was dropped and you can do whatever you need to do (e.g. delete the dropped temporary file and replace with the ones you originally wanted to drop. It is a far-from-perfect solution but might help. Better still, it might give someone an idea to come up with a better one!)
One downside is that you don't really know in which drive someone might drop the file and you may have to set up a watcher for several drives. And if you miss a drive (or a network path) then problems .....! Remember to dispose of the watchers after the drop.
There has to be a better way though. e.g. consider when you drag a file from a zip folder. The file is only extracted after the drop.
i don't think that is possible.

Categories