Is it possible to copy a file or a folder from one location to another without modifying its attribute data? For example if I have a folder on a network drive and it was created on 2/3/2007 and I want to copy it to my c: drive .. but leave the date/time stamp as 2/3/2007...is that possible?
I'm not sure if it is possible; however you can use the methods within System.IO.File and System.IO.Directory to reset these attributes back to what they were originally.
Specifically the SetCreationTime and SetModificationTime methods will be of most value to you in this case.
I did something as shown below:
File.SetCreationTime(tgtFile, File.GetCreationTime(srcFile));
File.SetLastAccessTime(tgtFile, File.GetLastAccessTime(srcFile));
File.SetLastWriteTime(tgtFile, File.GetLastWriteTime(srcFile));
When you copy a file, it will retain the modified date, however the created date will be changed. I doubt there will be an easy way to retain the created date.
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=net-7.0
The attributes of the original file are retained in the copied file.
Related
Good day!
I have some file called "*.dat" with text into it.
So, i try to create attribure "Version" ,but don't know how.
Can i do it via c#? Can you write some examples?
Such like this?
File.SetAttributes(path, attributes);
Thank you!
You can't. The only values allowed are from the list here
You're probably struggling because you can't add arbitrary information into a file. There are a known set of attributes you can change using the FileAttribute properties
What you would normally do is provide some information at the start of your file, typically called the file header. This then allows a custom reading implementation to read out the version, without having to read the rest of the file. This is quite standard practise with all the files you're used to, for example a WAV audio file:
The attributes you can change here are the NTFS attributes. Details here:
http://msdn.microsoft.com/en-us/library/system.io.fileattributes%28v=vs.110%29.aspx
Version is a resource embedded in an executable file. A similar question was asked here:
How do I set the version information for an existing .exe, .dll?
reating "custom" file attributes is not allowed.
You can only set one of the FileAttributes .
Example:
File.SetAttributes(#"C:\myfile.txt", FileAttributes.Hidden);
I would like to exclude a whole hierarchy of files and folders from the backup using
NSFileManager.SetSkipBackupAttribute()
The documentation is about "files". Can I also pass a path and all files and folders under that path - also those ones added afterwards - won't be backed up either or do I have to set the attribute on every individual file?
Like stated in the documentation the underlying API used (except for iOS 5.0.1) is NSUrlIsExcludedFromBackupKey and according to this answer it is recursive.
I am developing a WinForms application that stores the user's settings in a text file. I reference this save file when the user updates their settings as well as when the program starts up. Everything is working with the StreamWriters and StreamReaders, except that when I write data to the text file, it is automatically converted to a string since .txt files don't hold variable types.
One of the settings is the default directory to open files from, and the OpenFileDialog dialog only accepts Environment.SpecialFolder file paths. I'm having trouble parsing the string to a usable Environment.SpecialFolder. I realize I could check the value using the string.Contains(string s) method but I would like to know if there is a simpler way first.
SpecialFolder is an enumeration; to parse enumeration values, use Enum.Parse(typeof(Environment.SpecialFolder), "ValueGoesHere")
It sounds like you've written your own classes to manage user settings in a text file. You might want to consider using the application settings classes to do that for you. I think they handle enumerations for you, but I'm not sure.
My question is actually related to this post.
VB script + read files (only files with "log" name) and copy content files into one file.txt
I want to do exactly what he has done (combine *.log files that I have a user browse for) however I need them to be inserted into the new log file time wise. For example:
1.log (12:15:66)
2.log (10:09:33)
3.log (15:11:10)
I need the out put to be in the final.log file but in the order (2.log, 1.log, 3.log) because thats the order timewise they were created. I also will have different numbers of log files so it needs to either combine all in a directory or ask for each file until I don't specify anymore. I am going to be using C# also not VB like in the example.
Help is much appreciated!
Once the user has selected all of the logs that he wants to include, you can get the FileInfo for each file. Store those in a list and sort by timestamp. Then use a simple loop to copy each one to the output file.
I am creating an application to back up files from a source directory into a destination directory. I store the files information from the source and destination folders in separate lists then I compare the lists based on their size, date modified etc to see which files need to be copied.
Anyways the point is that I end up with a list of the files that need to be copied and I will like to know how much time is every file taking therefore I have tried the following techniques:
Technique 1
Technique 2
Thechnique 3 : the regular File.Copy("source....","Destination")
The first two techniques are great because I can see the progress. The problem is that when I copy some files with those techniques, the new file sometimes has different dates. I will like both files to have the same modified date and also the same creation date. Moreover if for whatever reason my program crashes the file that is being copied will be corrupted because I have tried copying a large file ( a file that takes about a minute to get copied in windows) if I exit my program meanwhile the file is being copied the file that is being copied sometimes has the same attributes and the same size so I want to make sure I don't have corrupted files in case my program crashes.
Maybe I should use aether techniques 1 or 2 and then at the end copy the attributes from the source file and assign those to the destination file. I don't know how to do that though.
FileInfo has members CreationTime and LastWriteTime that are settable - so you could settle for your preferring techniques and set the dates afterwards if that helps.
Have you considered just writing a shell script that calls robocopy? Any time I've had to run backup tasks like this, I just write a script -- robocopy already does the heavy lifting for me, so there's often no need to create a bespoke application.
A solution that I have but its long:
I know I can copy the file from the source and then name the file in the destination something else like "fileHasNotBeenCopiedYet" with attributes of hidden then when my program finishes copying the file change the name to the source name and copy the attributes and then latter I know that if a file with that name ("fileHasNotBeenCopiedYet") exists that means that the file is corrupted.