c# check files on server using web client - c#

C# 2008
I have using the WebClient DownloadFile method.
I can download the files I want. However, the client has insisted on creating different folders which will contain the version number. So the name of the folders would be something like this: 1.0.1, 1.0.2, 1.0.3, etc.
So the files will be contained in the latest version in this case folder 1.0.3. However, how can my web client detect which is the latest one?
The client will check this when it starts up. Unless I actually download all the folders and then compare. I am not sure how else I can do this.
Many thanks for any advice,

Create a page which gives you the current version number.
string versionNumber = WebClient.DownloadString();

Allow directory browsing in IIS and download the root folder. Then you could find the latest version number and construct the actual url to download. Here's a sample (assuming your directories will be of the form Major.Minor.Revision):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
using (var client = new WebClient())
{
var directories = client.DownloadString("http://example.com/root");
var latestVersion = GetVersions(directories).Max();
if (latestVersion != null)
{
// construct url here for latest version
client.DownloadFile(...);
}
}
}
static IEnumerable<Version> GetVersions(string directories)
{
var regex = new Regex(#"<a href=""[^""]*/([0-9]+\.[0-9]+\.[0-9])+/"">",
RegexOptions.IgnoreCase);
foreach (Match match in regex.Matches(directories))
{
var href = match.Groups[1].Value;
yield return new Version(href);
}
yield break;
}
}

This question might have some useful information for you. Please read my answer which deals with enumerating files on a remote server.

Related

FFProbe.Analyse getting the error 'The system cannot find the file specified' when given a string

I want to compress every video within every drive and its sub-directories so the code I have used so far finds each drive and looks for .mp4 locations. Then it uses that list of strings to compress each file but it comes up with this error at:
var mediaInfo = FFProbe.Analyse(filePath: d)
and
.FromFileInput(d, verifyExists: true)
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
I checked what the .Analyse needs and it is a string and d is a string which has the right path location C:\\Users\\Helix\\Desktop\\apartment\\5 Little Monkeys Swinging In The Tree.mp4" which I thought would work but it does not seem to like it. What am I doing wrong?
I am also curious as to if GetDrives() works on network drives? And if it does would two servers running this code conflict when grabbing the same file at the same time?
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MediaToolkit;
using MediaToolkit.Model;
using FFMpegCore;
using FFMpegCore.Enums;
namespace Video_Compressor_for_Servers
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CompAll_Click(object sender, EventArgs e)
{
//An empty list for later to collect strings
List<string> file = new List<string>();
//extract primary drives strings into a list since there could be more than just C:\
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
//The d.Name output has "Drive C:\" as the output. remove the "Drive " part first
var replacement = d.Name.Replace("Drive ", "");
/*Grab each files location with the Directory tool from the earlier IO libary.
GetFiles is a subcommand of directory with options in the brackets. These
options can be found in more detail by seaching for GetFiles C#*/
string[] files = Directory.GetFiles(#"C:\Users\Helix\Desktop\apartment\","*.mp4", SearchOption.AllDirectories);// replacement//AllDirectories
//Convert the array to a list
List<string> templist = files.ToList();
//Add them to the earlier empty list we made
file.AddRange(templist);
}
//Now that we have paths to all the files we need we can now compress them
foreach (string d in file)
{
var mediaInfo = FFProbe.Analyse(filePath: d);
//Open the video file with MediaToolkit
FFMpegArguments
.FromFileInput(d, verifyExists: true)
.OutputToFile(d, false, options => options
.WithVideoCodec(VideoCodec.LibX264)
.WithConstantRateFactor(21)
.WithAudioCodec(AudioCodec.Aac)
.WithVariableBitrate(4)
.WithVideoFilters(filterOptions => filterOptions
.Scale(VideoSize.Ed))
.WithFastStart())
.ProcessSynchronously();
}
}
}
}
You need to download both ffprobe.exe and ffmpeg.exe.
The executable is missing not the media one.

Get TFS information for locally mapped file or directory via WebApi

I want to use the Teamfoundation.SourceControl.WebApi to check for updates or local changes against our TFS Source Control.
I can gather information about changesets from an item which is committed TFS but I am not able to gather this information based on a local file path inside my mapped workspace.
Is it somehow possible without using the ExtendedClient?
I want something like this:
TfvcChangesetSearchCriteria tcsc = new TfvcChangesetSearchCriteria();
tcsc.ItemPath = #"c:\source\mappedtfs\MYPROJECT\src\MainWindow.cs";/*<--- localPath would be nice here*/
List<TfvcChangesetRef> changerefs = tfvcHttpClient.GetChangesetsAsync("MYPROJECT", null, null, null, null, tcsc).Result;
Microsoft.Teamfoundation.SourceControl.WebApi is a webapi which does not interact with local workspaces and files. If you want to get changesets with local items' path, use Microsoft.TeamFoundation.VersionControl.Client in the Client Library.
using Microsoft.TeamFoundation.Client;
using System;
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections.Generic;
namespace ConsoleX
{
class Program
{
static void Main(string[] args)
{
Uri url = new Uri("https://tfsuri");
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(url);
VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
IEnumerable<Changeset> cses = vcs.QueryHistory("Path here could be local path or server path", RecursionType.Full);
foreach (Changeset cs in cses)
{
Console.WriteLine(cs.ChangesetId);
Console.WriteLine(cs.Comment);
}
Console.ReadLine();
}
}
}

How to get all branches of repository with SharpSvn?

I'm trying to get all branches of repository by using SharpSvn but I can not find any method can do it.
Is it possible to get all branches by using SharpSvn?
I think Matt Z was on the right track, but that code doesn't compile. Here is an adjusted version that should work with the latest version of SharpSVN (as of Dec 2015).
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using SharpSvn;
....
private List<string> GetSVNPaths()
{
List<string> files = new List<string>();
using (SvnClient svnClient = new SvnClient())
{
Collection<SvnListEventArgs> contents;
//you can get the url from the TortoiseSVN repo-browser if you aren't sure
if (svnClient.GetList(new Uri(#"https://your-repository-url/"), out contents))
{
files.AddRange(contents.Select(item => item.Path));
}
}
return files;
}
I don't know anything about SharpSVN, but branches in Subversion are just directory trees -- there's nothing special about them.
If your repository follows they typical layout with three top-level directories (trunk/ branches/ tags/), you can just check out the /branches directory to get all the branches side-by-side.
The SharpSvn.SvnClient class has a GetList() function that works really well:
using (SvnClient svnClient = new SvnClient())
{
Collection contents;
List files = new List();
if (svnClient.GetList(new Uri(svnUrl), out contents))
{
foreach(SvnListEventArgs item in contents)
{
files.Add(item.Path);
}
}
}
Once you have the collection, you can get the path of each item at the location. You can also use the Entry object to get information concerning each item, including whether it is a directory or a file, when it was last modified, etc.

C#/.NET Server Path to default/index page

In my attempt to further future-proof a project I am trying to find the best way to retrieve the full path and filename of the index/default page in a web directory using C# and without knowing the web server's list of filename possibilities.
'Server.MapPath("/test/")' gives me 'C:\www\test\'
...so does: 'Server.MapPath(Page.ResolveUrl("/test/"))'
...but I need 'C:\www\test\index.html'.
Does anyone know of an existing method of retrieving the filename that the webserver will serve up when someone browses to that directory - be it default.aspx, or index.html, or whatever?
Thanks for any help,
fodder
ASP.NET has no knowledge of this. You would need to query IIS for the default document list.
The reason for this is that IIS will look in your web folder for the first matching file in the IIS default document list then hand off to the matching ISAPI extension for that file type (by extension) in the script mappings.
To obtain the default document list you can do the following (using the Default Website as an example where the IIS Number = 1):
using System;
using System.DirectoryServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (DirectoryEntry w3svc =
new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
{
string[] defaultDocs =
w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');
}
}
}
}
It would then be a case of iterating the defaultDocs array to see which file exists in the folder, the first match is the default document. For example:
// Call me using: string doc = GetDefaultDocument("/");
public string GetDefaultDocument(string serverPath)
{
using (DirectoryEntry w3svc =
new DirectoryEntry("IIS://Localhost/W3SVC/1/root"))
{
string[] defaultDocs =
w3svc.Properties["DefaultDoc"].Value.ToString().Split(',');
string path = Server.MapPath(serverPath);
foreach (string docName in defaultDocs)
{
if(File.Exists(Path.Combine(path, docName)))
{
Console.WriteLine("Default Doc is: " + docName);
return docName;
}
}
// No matching default document found
return null;
}
}
Sadly this won't work if you're in a partial trust ASP.NET environment (for example shared hosting).

How do I restore a file from the recycle bin using C#?

Moving files to the recycle bin and emptying the recycle bin are well documented, but how can a file be programmatically restored from the recycle bin?
There seems not to be a solution in pure C#. You most likely have to resort to P/Invoke.
This article presents a solution in C++ using the SHFileOperation API.
The only other reference to this beyond the previously mentioned link to codeproject that I can see mentions this:
Call SHGetFolderLocation passing CSIDL_BITBUCKET.
Then you can manipulate that folder as usual.
You'll have to create an interop for the SHGetFolderLocation function.
CSIDL_BITBUCKET being the CSIDL ("constant special item ID list") value for the virtual Recycle Bin folder. The quote is taken from here, and will involve interop with the Windows shell. MSDN also mentions that this function has been deprecated in favour of another in Vista.
Hope below code will work to restore the files. Please make sure, STA Calls only supported for shell calls
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using Shell32; //Reference Microsoft Shell Controls And Automation on the COM tab.
using System.Runtime.InteropServices;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
private static void Restore(object param)
{
object[] args = (object[])param;
string filename = (string)args[0];
string filepath = (string)args[1];
Shl = new Shell();
Folder Recycler = Shl.NameSpace(10);
var c = Recycler.Items().Count;
var _recycler = Recycler.Items();
for (int i = 0; i < _recycler.Count; i++)
{
FolderItem FI = _recycler.Item(i);
string FileName = Recycler.GetDetailsOf(FI, 0);
if (Path.GetExtension(FileName) == "") FileName += Path.GetExtension(FI.Path);
//Necessary for systems with hidden file extensions.
string FilePath = Recycler.GetDetailsOf(FI, 1);
if (filepath == Path.Combine(FilePath, FileName))
{
DoVerb(FI, "ESTORE");
break;
}
}
}
private static bool DoVerb(FolderItem Item, string Verb)
{
foreach (FolderItemVerb FIVerb in Item.Verbs())
{
if (FIVerb.Name.ToUpper().Contains(Verb.ToUpper()))
{
FIVerb.DoIt();
return true;
}
}
return false;
}

Categories

Resources