How to programatically do file versioning with SVN and .NET? - c#

We have a report generator. Daily, it writes its data into a excel file.
For reasons of version controlling and file data safety, we need to alter this file, and commit the changes into a repository.
Do you recommend any .net SVN API you've used?

You should take a look at the SharpSvn .NET library. You will probably need checkout and commit commands:
Checking out:
string wcPath = "c:\\my working copy";
using (SvnClient client = new SvnClient())
{
client.CheckOut(new Uri("http://server/path/to/repos"), wcPath);
}
Committing:
string wcPath = "c:\\my working copy";
SvnCommitArgs a = new SvnCommitArgs();
a.LogMessage = "My log message";
using (SvnClient client = new SvnClient())
{
client.Commit(wcPath, a);
}

You might be able to turn on Autoversioning for your repository. Since it uses WebDAV, you can treat the repository just like a network drive (Web Folder). And you can treat the file as a normal file, just open, modify, and save.
If it were me , I would create a new repository for the excel data files. I don't think I'd like my code being autoversioned :)

We are using tool that actually searching for installed TortoiseSVN in predefined locations and using it command-line api. If that is for Windows and it's for not redistribution - it might be easier to do.
Helpful code for cmd:
#echo off
if exist "%ProgramW6432%\TortoiseSVN\bin\TortoiseProc.exe" set patht=%ProgramW6432%
if exist "%ProgramFiles%\TortoiseSVN\bin\TortoiseProc.exe" set patht=%ProgramFiles%
if exist "%ProgramFiles(x86)%\TortoiseSVN\bin\TortoiseProc.exe" set patht=%ProgramFiles(x86)%
echo Placing SVN Commit
"%patht%\TortoiseSVN\bin\TortoiseProc.exe" /command:commit /path:"%CD%" /notempfile
If you still want to do that task from code - SharpSVN http://sharpsvn.open.collab.net is better choiсe

The simplest would be to spawn a process and call SVN.exe
to commit your changes.
Here is a similiar question that was asked.
Does anyone know of a good C# API for Subversion?
Here is another resource
SVN Libraries for .NET?

Related

Create App to download Changeset in TFS

Im not sure if this is even possible but I am looking to be able to create an app (VB or C#) that will essentially replicate the behaviour of the 'View history' of a selected TFS Project. I will want to be able to browse the Changeset, select one and download the solution into a specified location.
I have not had a chance to take a proper look yet, but I assume it is straight forward to get the ChangeSet history from the SQL database, but I am not sure where to look next to be able to download the associated solution/project. Is it something similar to Filestream?
If anyone has covered this in the past, I would love to hear about how you got the project files.
Try with following code (I assume you are using TFVC version control):
using System;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace API {
class Program
{
static void Main(string[] args)
{
string vsuri = "http://xxx.xxx.xxx.xxx";
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(vsuri));
tpc.EnsureAuthenticated();
VersionControlServer vcs = tpc.GetService<VersionControlServer>();
Workspace ws = vcs.CreateWorkspace("NewWorkSpace");
ws.Map("$/ProjectName","C:\\LocationPath");
int changesetversiontoget = 111;
ChangesetVersionSpec cvs = new ChangesetVersionSpec(changesetversiontoget);
ws.Get(cvs,GetOptions.GetAll);
}
} }
The code will create a work space and then get all the files with a specified version like you get a version from Team Explorer\Source Control.
Just as Daniel mentioned, the best way to achieve what you need is through TFS API.
You can follow below links:
Using TFS API - how to get previous version of a file given a Changeset
Get Changeset and all it's changes on TFS using c#
If you want to download solution files for a given changeset, this could be achieved through web portal, detailed steps please check below link:
Download code of a specific changeset in TFVC

Dumping an SQLite database stored as dll file?

A little background first: I need to extract a database from an SQLite dll file for my client who has burned bridges with the previous dev.
I have these 4 files sitting on my desktop:
System.data.SQLite.Linq.dll
System.data.SQLite.dll
SQLite.Interop.dll
Payroll.exe
I am also given the password for the SQLite DB which Payroll.exe uses to interact with the database. With this is it possible for me to dump an SQL file (so I can work with something familiar)?
Major problem is that I have no experience with C#, .NET stuff and DLL files (I know it stands for Dynamic Link Library!)
I'm willing to dabble and learn some C# to do this.
As suggested, I decompiled the source and found some interesting code:
namespace Payroll
{
internal class Payroll_Database
{
private static string ConnectionStr = "Data Source=.\\System.Data.SQLite.Linq.dll; Password=******!";
private static SQLiteConnection Connection = new SQLiteConnection();
It seems certain that System.Data.SQLite.Linq.dll is the database file. I have trouble opening it with tools such as SQLite Browser. I have tried renaming it to a .db, but again no dice.
Your database won't be in the dll and exe files you list.
It should be in a separate file, possibly with an extension like .sqlite, .sqlite3, .sdb3 .db or .db3 (but this is not mandated).
If you can locate the db file, you can open it with any sqlite client (e.g. you can use the firefox sqlite-manager plugin), and extract the data as required.
You could probably find the database filename from the Payroll.exe source code, which you can read by opening Payroll.exe in ILSpy.
If you cannot locate the db file, you will be unable to retrieve the data. In which case, the best you can achieve with the files you list would be to determine the database schema by reading the payroll application's source code.
Update:
Now you've found the connection string, it looks possible that the database could indeed be the System.Data.SQLite.Linq.dll file, and the developer has given it a name to match a known .NET assembly as some kind of security through obscurity.
If that is indeed the db file you should be able to open it directly in a SQLite client application like the firefox sqlite-manager plugin I mentioned above. If you can't open it in a SQLite client application, then you can to check to see if it is a .NET assembly after all by opening it with ILSpy or Microsoft's MSIL disassmebler.
Update 2:
According to this SO question, you should be able to open your encrypted db file with the free SQLite2009 Pro Enterprise Manager.

SharpSvn GetFileVersions revision range, only one file version returned

I'm trying to use SharpSvn to read the contents of two revisions of a file. When I run the following code the fileVersions collection only contains one item..
var svnClient = new SvnClient();
var revisionInfo = new SvnFileVersionsArgs
{
Start = 80092,
End = 80093
};
Collection<SvnFileVersionEventArgs> fileVersions;
svnClient.GetFileVersions(
new SvnUriTarget("https://DbDiff.svn.codeplex.com/svn/DbDiffCommon/DataAccess/SqlCommand11.xml"),
revisionInfo,
out fileVersions);
However I would expect it to include two items. Using TortoiseSVN I can see that the file changed in revision 80088, so I would expect to get this version when I use Start = 80092..
Using Start = 80091 doesn't help either..
The problem is not in your code but in the SvnBridge software used by codeplex. (They store the data in TFS and provide access via the bridge instead of using a real Subversion backend).
The bridge software doesn't implement this api properly. (I added an issue on it years ago, but as far as I can tell it was never fixed).
Subversion itself only uses this api for 'svn blame' (/praise/annotate), so I think the SvnBridge developers didn't care enough to fix this.
In AnkhSVN I detect the case of just receiving one file and then use SvnClient.Write() to obtain the file the slow way :(

How to create Application.exe.config in application directory

So I recently updated my application to support a new feature. In the past if the configuration file was deleted by the user it wasn't a big deal. This new feature requires it to exist, and one of the requirements is that, the file exists in the application's installation directory.
I have notice when the file is deleted ( depending on variables I have not figured out ) I get a .NET notification that the configuration file is missing or corrupt. Currently my program then crashes ( I still have to figure out how to duplicate this behavior ) which is the reason for this question.
I am familar with ConfigurationManager. I am having trouble writting the file once the default values are loaded. Forcing a Save for some reason does not seem to recreate the file, at least not in the installation directory, which is a requirement.
I am looking for guidence on how to handle this corner case in an elegant manner. I would post code, honestly its just all failed attempts, which while my attempts do generate a file the contents are not the settings I am looking for.
I am willing to post anything that might be able to help.
Stop using the built-in config support and just use write/read to a file called something.exe.config using the standard XML classes and if that gets deleted, just re-create it from values hard-coded in the executable.
The config file support is supposed to make things easier, if you need to do stuff where it makes things difficult, don't use it.
Something like
var wcfm = new WebConfigurationFileMap();
Configuration newConfig = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
newConfig.Save();
doesn't work?
You dont. Under normal conditions the program can not write into it's install directory - this is a standard windows security issue and the reason why app application data should reside ni external (from the exe's point) driectories.
If an admin deletes the config file, crash, ask for reinstall. There is nothing you can RELIABLY do, as you can not assume you can write into the folder at runtime. A message followed by an event log entry is the best approach here. Users are not supposed to delete parts of the application.

C# to format (indent, align) C# properly

We have a code generator that munges the schema of a given database to automate our inhouse n-tier architecture. The output is various C# partial classes, one per file.
In the code to munge all the strings, we try and keep on top of the indenting and formatting as much as possible, but invariably when you come to open the file in Visual Studio the formatting is awry. A quick ctrl-k, ctrl-d fixes it, but obviously this reformatting is lost the next time the class is generated.
What I'd like to know, is if there's a way I can somehow automatically format the contents of the textfile in the same way Visual Studio does?
Pseudocode
Create "code" object, passing text file to constructor
Invoke "format" method
Re-save text file
Any help greatly appreciated.
EDIT:
I should clarify - I want to be able to invoke the formatting from my C# code that creates the textfile containing my generated C#. The format of the code can be standardised (doesn't have to be per-developer), and I don't want to have to install any 3rd-party apps.
I seem to remember there's a namespace containing loads of classes for creating C# in C#: http://msdn.microsoft.com/en-us/library/system.codedom(VS.80).aspx, but I'm not sure if it contains any classes that could help.
FURTHER EDIT:
My code generator is a winforms app deployed via a click-once install. It's used by many developers in-house. I need a solution that doesn't require each developer to have a tool installed on their machine.
To properly indent code programmatically you would need Microsoft.CodeAnalysis.CSharp nuget package and .NET framework 4.6+. Sample code:
public string ArrangeUsingRoslyn(string csCode) {
var tree = CSharpSyntaxTree.ParseText(csCode);
var root = tree.GetRoot().NormalizeWhitespace();
var ret = root.ToFullString();
return ret;
}
One-liner:
csCode = CSharpSyntaxTree.ParseText(csCode).GetRoot().NormalizeWhitespace().ToFullString();
You may also use NArrange to sort methods in your cs file, organize usings, create regions, etc. Note that NArrange does not indent anything.
Take a look at Narrange.You'll probably need to automate these things as part of the build.
Not sure if it meets all your requirements though.
To quote:
NArrange is a .NET code beautifier
that automatically organizes code
members and elements within .NET
classes.
You can use CodeDOM and the CSharpCodeProvider. It is all in the namespaces Microsoft.CSharp and System.CodeDom.
Her is an example of a property:
StringWriter writer = new StringWriter();
CSharpCodeProvider provider = new CSharpCodeProvider();
CodeMemberProperty property = new CodeMemberProperty();
property.Type = new CodeTypeReference(typeof(int));
property.Name = "MeaningOfLifeUniverseAndEverything";
property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(42)));
provider.GenerateCodeFromMember(property, writer, null);
Console.WriteLine(writer.GetStringBuilder().ToString());
This code will generate:
private int MeaningOfLifeUniverseAndEverything {
get {
return 42;
}
}
The CodeDOM is a quite chatty way to generate code. The good thing is that you can generate multiple languages. Perhaps you can find a Erlang.NET CodeProvider?
You might be able to do a few shortcuts by using CodeSnippetExpression.
Only if you're running the code generator as a VS add-on - each developer is going to have different settings.
Here's how to do it from the context of a macro or add-in:
var dte = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.8.0");
dte.ExecuteCommand("File.OpenFile", filename);
dte.ExecuteCommand("Edit.FormatDocument", filename);
dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes);
Warning: As #Greg Hurlman says, the output will vary depending on the user's current options.
Edit:
unfortunately your method requires me to have an instance of VS running alongside my winforms app. Can you think of a way to create an instance of VS from within my app (if that's even possible)?
I think it might be possible to do from within your Win.Form app. However, you'll have to have Visual Studio installed on the machine running the code.
Try this:
var dte = (EnvDTE80.DTE2)Microsoft.VisualBasic.Interaction.CreateObject("VisualStudio.DTE.8.0", "");
dte.ExecuteCommand("File.OpenFile", filename);
dte.ExecuteCommand("Edit.FormatDocument", filename);
dte.ActiveDocument.Close(vsSaveChanges.vsSaveChangesYes);
Keep in mind that you'll need references to the EnvDTE80.dll assembly.

Categories