How to read an non-shared file - c#

I am making a data backup program. I need to copy files from one folder to another, while some files are still being used by a running process. It's OK when the running process shares file access. But I will get exception if files are not shared. (FileShare.None)
I am wondering if there is any way to read a non-shared file in C#.
Thanks,

C# under the hood uses the file access operations your operating system provides, including its file sharing behavior. So I'm afraid C# cannot go beyond this.

There is a technique if you are using Vista or later. You can use the Volume Shadow Copy feature. However, getting to work in C# will be tedious, and all that I can provide for help are references. However, if you wanted to administrate a solution instead of developing one, you could use Windows Backup to copy the files on a schedule.
Links:
Shadow Copy Explained
Shadow Copy SDK
Video Presentation on Shadow Copy

Related

Programmatically copy in-use files

In my C# code I want to be able to use some Shadow Copy mechanism in order to copy files that are being used by another process.
I've seen that solutions exist on the web, in enterprise or command line tools. But could it be done programmatically in order to mimic a simple file copy?
It appears that alphavss does what you want. The sample file VssBackup.cs here seems to do exactly what you want.
This class encapsulates some simple VSS logic. Its goal is to allow a user to backup a single file from a shadow copy (presumably because that file is otherwise unavailable on its home volume).

extracting a file without letting the user access it

I was always pretty impressed by those programs that you could install by executing one installer file, which would then extract all the other required files to run the actual program.
And even now im still wondering how you would code a program that extracts files that are literally still inside the program ( so not in some kind of zip) , i've seen tons of installers for games who have this. I need this cause I want to extract a file on the right moment without giving the person who uses the program the ability to delete the file before its extracted, this may seem vague, but I hope i've informed you enough.
I'm just going to say that building an installer is difficult.
I'd recommend using NSIS: http://nsis.sourceforge.net/Main_Page
As for creating a file the user can't access, create a temp file with the correct read/write permissions, extract the data to the temp file, then copy the file where it needs to go.
Extract happens without the user interfering, and copy protection is handled by the OS.
What about changing the build action for the file you want to hide to Embedded Resource, or something like that that compiles the file inside the dll/exe?
Executable program is just file, So you can append any data at you executables. (works for my c++ compiled program)
$ cat executables some_blob_data > new_executables
Since argv[0] of main() is name of your file, you can use this to acess data in this file itself (works for c or c++ and likley for other languages to)
A really simple way to do this is to use your archive tool or one of the dozens of already made installers. WinRar, WinZip and most others allow the creation of self extracting exe files. I know you've said that is not what you want but if you make sure to make it auto exec your installer app and remove all of the temporary files when you're done it really can be very fool proof and professional looking. Obviously, the various installer applications are able to do what you're wanting.
If you really want to do this yourself the easy solution is going to most likely be dependant on your IDE software and language. The generic answer is that you'll need a compression library which can accept a stream as input. Then you'll want to make your actual files into resources inside your installer app. At that point it's just a matter of telling the compression library to read from a stream which is pointed at the resource. How that is done varies greatly from language to language.

Create a folder backed by software rather than OS?

I recently got put on a project where they're having issues with too many files in a folder slowing down access. I believe it is 10,000+ files in a single folder where windows starts to slow down access, we have something on the order of 50,000. All the files are small and most of the time we only need to access the newest .1-2.% of them via windows file and print sharing. I'd look into dividing the files into subfolders, except that there is a bunch of legacy code that is only able to look at a single folder.
My idea - I don't know if it is possible or even plausible - is to create a small program that buffers the newest .1-.2% files in memory, and retrieves the rest from disk as needed.
I had thought that years ago I'd read of a protocol that could simulate a folder on a hard drive. Is it possible?
Is there something out there that already does this? Is there a better option without major changes to the system?
What to other systems use for serving up a large number of files? Is there some other product that serves files that we could map as a network drive? Or some way to blend 2 folders so they look like one?
Putting aside the "correct way to solve this problem" for the moment, what you're looking for is called "Shell namespace extensions". There are several .NET resources for writing these explorer extensions.
http://namespaceextension.codeplex.com/
http://www.codeproject.com/Articles/1649/The-Complete-Idiot-s-Guide-to-Writing-Namespace-Ex
http://www.codeproject.com/Articles/13515/A-Namespace-Extension-Toolkit
And perhaps many more.
Of course - we must remember why it isn't a good idea to write explorer extensions in .NET.
Hope this helps.

Copying .svn File?

I'm trying to automate some stuff in SVN, and part of that is copying a .svn file (the one that contains where a directory should update their files from) to a local drive.
I'm using the following code:
File.Copy(#"X:\SVN\.svn", #"C:\SVN\.svn, true");
And I get the following error:
Access to the path 'X:\SVN\.svn' is denied
Am I just not allowed to move these types of files around? I know by default they are hidden, so maybe that's what's going on. Or is there a way in C#, I can just create a new .svn file so I don't have to bother with permissions and the like?
Thanks!
Last time I checked, .svn was a directory. You probably can't copy a directory with File.Copy, or the directory is in use (sharing lock).
Have a look at ProcessExplorer's find function (in Sysinternal Suite) to find out which process uses it (perhaps TortoiseSVN?)
Perhaps schedule your replication using Rich Copy which is a RoboCopy clone from Microsoft that will copy permissions, incremental update etc.
Typically this is because you have tortoise running, and the tortoise cache has the file locked. If you shut it down in task manager you can do whatever you'd like with hit.
That said, what is the purpose of copying it? Are you cloning the project?
If you are on Windows and using TortoiseSVN, then the .svn is actually a directory so you can't use File.Copy on it.

How To Store Files In An EXE

Alright, so I'm working on programming my own installer in C#, and what I'd like to do is something along the lines of put the files in the .exe, so I can do
File.Copy(file, filedir);
Or, if this isn't possible, is there another way of doing what I am attempting to do?
I wouldn't code my own installer, but if you truely want to embed files into your assembly you could use strongly typed resources. In the properties dialog of your project open up the "Resources" tab and then add your file. You'll then be able to get the file using:
ProjectNamespace.Properties.Resources.MyFile
Then you'll be able to write the embedded resource to disk using:
System.IO.File.WriteAllBytes(#"C:\MyFile.bin", ProjectNamespace.Properties.Resources.MyFile);
Honestly, I would suggest you NOT create your own installer. There are many many issues with creating installers. Even the big installer makers don't make their own actual installers anymore, they just create custom MSI packages.
Use Mirosoft Installer (MSI). It's the right thing to do. Make your own custom front-end for it, but don't recreate the already very complex wheel that exists.
UPDATE: If you're just doing this for learning, then I would shy away from thinking of it as "an installer". You might be tempted to take your "research" and use it someday, and frankly, that's how we end up with so many problems when new versions of Windows come out. People create their own wheels with assumptions that aren't valid.
What you're really trying to do is called "packaging", and you really have to become intimately familiar with the Executable PE format, because you're talking about changing the structure of the PE image on disk.
You can simulate it, to a point, with putting files in resources, but that's not really what installers, or self-extractors do.
Here's a link to Self-Extractor tutorial, but it's not in C#.
I don't know enough about the .NET PE requirements to know if you can do this in with a managed code executable or not.
UPDATE2: This is probably more of what you're looking for, it embeds files in the resource, but as I said, it's not really the way professional installers or self-extractors do it. I think there are various limitations on what you can embed as resources. But here's the like to a Self-Extractor Demo written in C#.
I'm guessing here, but if you are trying to store resources in your application before compilation, you can in the Project Explorer, right click a file you would like to add, chose properties and change the type to Embedded Resource.
You can then access the embedded resources later by using the instructions from this KB:
http://support.microsoft.com/kb/319292
in case you simply want to store multiple files in a single file storage (and extract files from there, interact etc.) you might also want to check out NFileStorage, a .net file storage. written in 100% .NET C# with all sources included. It also comes with a command line interpreter that allows interaction from the command line.

Categories

Resources