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

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.

Related

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

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.

Pass Command Line argument to Beyond Compare

I have a list of files in a dataGridView that I would like to be able to select 2 of them (I can figure out how to check for the selectedRows count) and pass those files to Beyond Compare 3 for comparison. I looked through their Support page and I couldn't find a way to do that.
In the program I need to open the application (BC3) and pass the application the 2 file paths in an argument to start the comparison.
I am just using System.Diagnostics.Process.Start(bc3.exe path) to launch beyond compare.
Look at their support page for configuring version control systems. The general syntax seems to be
"C:\Program Files\Beyond Compare 3\bcomp.exe" %1% %2% /lefttitle="%3%" /righttitle="%4%"
So it looks like you need to pass four arguments, which are the left and right file, and then the left and right title. So you'll want to use the two-argument form of Start
System.Diagnostics.Process.Start("C:\Program Files\Beyond Compare 3\bcomp.exe",
"file1.txt file2.txt /lefttitle=\"foo\" /righttitle=\"bar\"")
I don't have BC3 installed at the moment so I haven't tested the above, but it should be very close.
There are various other questions on SO for integrating BC with git, svn, etc. They will give you other examples of starting BC from the command line.
The following works for me.
string bc3 = #"C:\Program files (x86)\Beyond Compare 3\bcompare.exe";
Process.Start(bc3, #"c:\temp\File1.cs c:\temp\File2.cs" );
or if your filenames have spaces in them
Process.Start(bc3, #"""c:\temp\File 1.cs"" ""c:\temp\File 2.cs""" );

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

When checking if a file is MP3 is checking the filename string with an .EndsWith good enough?

I'm doing this:
private void LoadSoundFile()
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.FileName.EndsWith(".mp3"))
{
txtFileName.Text = openFileDialog1.FileName;
}
else
{
MessageBox.Show("Currently Musicality only supports MP3 files.", "Unsupported file chosen.");
}
}
}
Is there a better way of checking file types or am I doing it the right way?
Having the .mp3 extension doesn't mean it is an mp3, but not having it is an (acceptable) indication that it isn't.
At some point you will call some API to play the file, and it will fail. When it does, you know it's not a playable file. So make sure you handle that with some decent UI too.
Your question seems to ask if the right way to check if a file is MP3 is to look at the end of the filename. As others have said, the answer to that is no. Matt Warren's post can help you if you want to look into the file to see if it is actually mp3 format.
But your comment on Eran Betzalel's answer makes me wonder if you are asking generally whether the right way to check a file extension is to use String.EndsWith().
One thing to notice is that EndsWith(string) is case-sensitive, so the results of:
EndsWith("mp3")
EndsWith("Mp3")
EndsWith("MP3")
and
EndsWith("mP3")
don't all give the same answer. A better test might be:
if (Path.GetExtension(openFileDialog1.FileName).ToLower() == "mp3")
if all you care about is the file extension and not the file contents.
If you actually want to analyse the file (to check if it really is a .mp3) you'll need to look at the specification so you parse it correctly. Here is a good place to start and there is some more info here. This article on the CodeProject goes even further and extracts ID3 tags as well as the header.
This will be better than just checking that the extension is ".mp3", but it's a lot of extra work so it has to be worthwhile.
It really depends on the nature of your program. I think that if you are not developing a security related application, then you can use the simple extension check.
No, because file extension is simply an indicator, it is not a reliable guide to what the file is or contains.
I can name i music file as mySong.zzz and it will still play in Winamp. When you load it you should sample the start of the file to see if it really is an mp3.
You can also set a filter on your open file dialog so that it only allows the user to select mp3 files:
openFileDialog1.Filter = "mp3|*.mp3|All Files|*.*";
I guess the proper way to actually check if it's an MP3 file (this requires that the file be opened) is to look for "magic numbers", sequences of bytes within the binary data that always occur. In this case, you can use the ID3 tag's magic number: ID3v1 tags are stored in the last 128 bytes of the file starting with the bytes "TAG" (hexadecimal "544147"), while ID3v2 tags are stored at the beginning of the file, so the first 3 bytes of the file are "ID3" (hexadecimal "494433"). I don't know if the MP3 frames themselves have simple magic numbers like this. Obviously, this method requires the file to be opened, which could make a scan of a large number of files slower.
If you want to be sure, load the file with this lib http://sourceforge.net/projects/id3dotnet/ it will fail with an exception if not an mp3. Simply create an Id3.Net.Mp3File with filename or stream in constructor an see if it throws an exception

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