C# and WebClient.UploadFileAsync: howto retain modified date? [duplicate] - c#

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

What is a surefire way of getting a file's true creation time?

I need to know when a file that I'm downloading was created, or last written to. Just the date is all I need (such as 6/17/2011). Normally, the file's date can be sussed out by its name, such as "DonQuixoteWasRight.2011-06-17.log"
The problem is that the file can have all sorts of different naming formats, perhaps not even containing the date, such as "SanchoPanzaWasLeft.txt"
I thought maybe the FileInfo class would ride in to the rescue, but with this code:
FileInfo fInfo = new FileInfo(SelectedFileName);
//DateTime when = fInfo.CreationTime; //or CreationTimeUtc?
DateTime when = fInfo.LastWriteTime; //or LastWriteTimeUtc?
return when;
...It simply returns the time that I accessed the file (although I neither created it nor explicitly wrote to it). Neither CreationTime nor LastWriteTime return the true CreationTime or LastWriteTime of the file. Is there a way to find out?
It sounds like you're trying to find out when the file was modified on the server.
Unless the server explicitly tells you somehow, there is no way to find that out.
There is no true way to figure this out, whether the file is on a server or even if it is on your local computer, because the last modified date and other metadata can be changed by users.

Is it possible to swap out large localization strings to their own files?

Currently I'm finishing my very first iPhone application with MonoTouch. Localization through the "*.lproj" folders works as expected.
Having an UIWebView that displays some user guidelines, I'm populating this one with the LoadHtmlString() method. (I.e. no internet connection is required).
Since the text is a bit longer, I do not want it to be placed inside the "Localizable.strings" file but being swapped out to a completely separate file (as I'm doing it for Windows .NET applications, too):
In the above screenshot, I would have one "help.html" file inside each language folder and call the LoadHtmlString method to read from the appropriate file in a way that would be similar to NSBundle.MainBundle.LocalizedString.
My question:
Is it possible to have per-language files and access them from within a MonoTouch application?
Follow-up to Dimitris' solution
Based on Dimitris' solution, I solved it by this code:
var localizedHtmlFile = NSBundle.MainBundle.PathForResource("help", "html");
var text = File.ReadAllText(localizedHtmlFile);
helpTextView.LoadHtmlString (text, null);
Yes, of course it is possible. You can get the path of the localized file like this:
string localizedHtmlFile = NSBundle.MainBundle.PathForResource("help", "html");
You can use the PathForResource method for various different types of resources (PDFs, images, etc.). The first parameter is the file name and the second one is its extension. Check the other overload of the PathForResource method for more options.

Read from a file that changes everyday in C#

I want to automate a program that reads a file, processes it and then write it to a new file. The problem is that a new file comes in every day, and the contents are similar, the input file and output file names will change daily. The file name will be in the following format: SAPHR_Joiners_20110323. As you can see the first part of the name will be constant but the date will be unique...... How would i be able to do this?
Thanks alot guys
If you want to read the latest file in a folder, you could query the created date, using System.IO.File.GetCreationTime
In code:
string myFile =
Directory.GetFiles(#"C:\Temp")
.OrderBy<String, DateTime>(file => File.GetCreationTime(file))
.First();
However, if you know that the file-name will follow a strict naming convention, then it is better to access the file by generating the file name as other answers suggest.
Can't you just generate the filename dynamically in your program, and then open the corresponding file? So something like this:
string filename = "SAPHR_Joiners_" + DateTime.Now.ToString("yyyyMMdd");
string[] filecontents = File.ReadAllLines( filename );
Use a FileSystemWatcher class to look for new incoming files if you want prompt respone, otherwise just locate the file based on a current date. If you have further problems, let us know.
Back the days of VB6 one technique that still is in use this days is the folder monitoring
You keep checking if a folder has files, every x in x minutes, or in your case, every day at XX hours for example.
and you could create a Service from your program and that will insure that it will run every time (as long as the machine is on) :)
Those days, in VB6, we didn't had so much as you have today, so, for watching a folder for specific file types (or anything at all) *.* you can use the System.IO.FIleSystemWatcher (example in that page), and to process the file, just use System.IO.TextReader for example

c# SharpSVN, how does one get a copy of specific revision files?

I was looking for something in SharpSVN that will do the equivalent of "Save revision to..." in the TurtoiseSVN GUI. I have been trying to find out how to do this with no luck. Currently I am looking at:
Note: logentry is a SvnLogEventArgs after I called client.GetLog(uri, arguments, out logitems);
foreach (SvnChangeItem svnChangeItem in logentry.ChangedPaths)
{
// I would think I could do something like svnChangeItem.SaveRevsionTo()
}
The SvnChangeItems store basically the exact information that is shown in TurtoiseSVN. When you right-click there it allows you to save the selected revsision file which is what I am hoping to do with SharpSVN (I do not want to actually check out the file, just get a copy of the file at that revision). Thanks.
Use SvnClient.Export, passing in a SvnUriTarget constructed with the repository url and desired revision number.

List the contents of a file on the form in C#?

I was just wondering how do you display the conetnts of a chosen folder on a ListView or something for example so the files can be individually be selected (and multiple files)
At the moment i have a folder dialog where the user chooses their desired path and yeah have stopped there :S
Given the string path you can use
Directory.GetDirectories
and
Directory.GetFiles
to retrieve the contents of a folder.
I'm going to focus on your statement : "a Listview or something," and talk about the "something" scenario :)
Why aren't you using the built-in control 'OpenFileDialog : you can set the 'MultiSelect property to 'true and select all the files you like, you can filter the files that appear in complex ways, etc. : it's there, it's "free," it works.
If you specifically do not want to use this control for reasons like, for example, you want the list files to remain visible (i.e., not to be a modal interface) at all times, I suggest you clarify your original question to reflect that. The more you tell us exactly what you want, the more focused the answers you can get.
regards Bill,
System.IO.Directory.GetFiles(<filepath>)
will return a string array which you can iterate through and display file names. It can be passed a true boolean value as well if you wish to do a recursive directory search.
If you wish to display directories as well, you will need to use
System.IO.Directory.GetDirectories(<filepath>)
If you just call ListView.Items.AddRange(Directory.GetFiles(#"c:\temp"); the names of all files in c:\temp will be shown in the ListView.
All the cool kids use Linq :)
var fileList = new DirectoryInfo(#"C:\").GetFiles().Where(file => file.Extension == ".txt");
foreach (var file in fileList)
{
// Do what you will here
// listView1.Items.Add(
}
This just gets text files in the C:\ drive, but you can tweak as necessary

Categories

Resources