I'm adding files to a custom vpp folder which uses the default VirtualPathNonUnifiedProvider:
var pageRootDirectory = (UnifiedDirectory)GenericHostingEnvironment.VirtualPathProvider.GetDirectory("~/CatalogImages/");var versioningDirectory = pageRootDirectory as VersioningDirectory;VersioningFile versioningFile = (VersioningFile) pageRootDirectory.CreateFile(resizedFileName);
I thought it whould always create a new version of a file when I use the create file but it doesn't, because the second time I create the same file it will throw an exception.
My question is how do I create a new version of a file?
Thanks in advanced,
The VPP needs to be a VirtualPathVersioningProvider. You can check that in EPiServer.config. And this code should work:
UnifiedDirectory unifiedDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(path) as UnifiedDirectory;
UnifiedFile unifiedFile = HostingEnvironment.VirtualPathProvider.FileExists(filePath)
? HostingEnvironment.VirtualPathProvider.GetFile(filePath) as UnifiedFile
using (Stream stream = unifiedFile.Open(FileMode.OpenOrCreate, FileAccess.Write))
// Write to the stream
Thanks for the quick reply. What I whould like to achieve is if the file already exists, that I can create a new version of this file. How can I do this?
If you're using a VirtualPathVersioningProvider, there will be a new version of the file, if it exists already.
So on the moment I see that the file already exists and I use the GetFile method I get a new version of this file? Don't I have to manualy create one? How do I save the new version of this file without overwriting the current.
No you don't get a new version, but one will be created when you write to the stream. You don't have to do more than write to stream, it just works magically.
Ah ok it's getting clear for me thanks! I'll give it a shot tomorrow and let you know.