What is the namespace of Google.Apis.Authentication; - c#

I'm trying to use this link to download a file from Google Drive using dotnet.
The problem is, I can't find this namespace in the nuget - using Google.Apis.Authentication; .
I'v downloaded everything that has the name 'Google' in nuget, but no luck.
Any idea where it can hide? Thanks

To access Google drive the only nugget package you need to download is PM> Install-Package Google.Apis.Drive.v2. It will automatically add anything else you need.
My download from drive method
/// <summary>
/// Download a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/get
/// </summary>
/// <param name="_service">a Valid authenticated DriveService</param>
/// <param name="_fileResource">File resource of the file to download</param>
/// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
/// <returns></returns>
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
code ripped from google drive sample project

I think that there is a better sample for you (in the official samples repo, https://github.com/google/google-api-dotnet-client-samples/blob/master/Drive.Sample/Program.cs#L154).
...
await DownloadFile(service, uploadedFile.DownloadUrl);
...
/// <summary>Downloads the media from the given URL.</summary>
private async Task DownloadFile(DriveService service, string url)
{
var downloader = new MediaDownloader(service);
var fileName = <PATH_TO_YOUR_FILE>
using (var fileStream = new System.IO.FileStream(fileName,
System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var progress = await downloader.DownloadAsync(url, fileStream);
if (progress.Status == DownloadStatus.Completed)
{
Console.WriteLine(fileName + " was downloaded successfully");
}
else
{
Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ",
fileName, progress.BytesDownloaded);
}
}
}
More documentation about media download is available here:
https://developers.google.com/api-client-library/dotnet/guide/media_download

Related

Web authentication issue using Script Task in SSIS

Hi I am using Script Task to download a file from a website but I seem to be having problems. I can't get past the web page which asks for Username and password. In the Connection Manager Editor I have listed the URL address and User Name and Passwords and the Test Connection Succeeded. So I am not sure what the problem is. Any help is appreciated.
namespace ST_054ab1f1837a4b9d8f167cfd91109f9b
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public void Main()
{
try
{
// Logging start of download
bool fireAgain = true;
Dts.Events.FireInformation(0, "Download File", "Start downloading " + Dts.Connections["HTTP"].ConnectionString, string.Empty, 0, ref fireAgain);
// Get your newly added HTTP Connection Manager
Object mySSISConnection = Dts.Connections["HTTP"].AcquireConnection(null);
// Create a new connection
HttpClientConnection myConnection = new HttpClientConnection(mySSISConnection);
// Download file and use the Flat File Connectionstring (D:\SourceFiles\Products.csv)
// to save the file (and replace the existing file)
myConnection.DownloadFile(Dts.Connections["FileName"].ConnectionString, true);
// Logging end of download
Dts.Events.FireInformation(0, "Download File", "Finished downloading " + Dts.Connections["FileName"].ConnectionString, string.Empty, 0, ref fireAgain);
// Quit Script Task succesful
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
// Logging why download failed
Dts.Events.FireError(0, "Download File", "Download failed: " + ex.Message, string.Empty, 0);
// Quit Script Task unsuccesful
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}
}

Command line SFTP "Local to local copy not supported" error

I'm running a program written in C# from the command line as administrator that should generate the batch file (which it does), and then it should sFTP the file to a remote site. I have verified the username and password are correct. When I run the utility (C# program) to do this it says it's transferring the file and then immediately gives me this
ERROR: Local to local copy not supported.
However, I can manually (through Filezilla) move the file from our server to their site. It's probably something silly, but I just can't seem to figure it out. Any help is appreciated!
There are many files to this program, but here is where the most of the FTP stuff is in the code. I hope it helps:
if (pars.ContainsKey("ftp"))
{
var env = (pars.ContainsKey("ftp") ? pars["ftp"] : null) ?? "default";
entities = entities ?? new SBLStagingEntities();
settings = settings ?? new SettingReader(entities, env).GetSetting();
var filename = Path.GetFileName(pars["path"]);
Console.WriteLine("Transfering {0} using sFTP ................................\t\t", filename);
var processors = new SblFtpTransport(settings);
processors.ProcessFile(pars["path"]);
Console.Write("sFTP Done\n");
}
///-----------------------a different class that is called from the first one------///
public SblFtpTransport(Settings settings)
{
_settings = settings;
}
/// <summary>
/// this method is called by file watcher for each new file dropped in the watched folder
/// </summary>
/// <param name="file"></param>
public void ProcessFile(string file)
{
var fileName = Path.GetFileName(file);
if (!File.Exists(file) || string.IsNullOrEmpty(fileName))
{
Console.Error.WriteLine("file does not exist");
return;
}
//ftp the file and record the result in db
var result = FtpFile(file);
Log(fileName, result);
Console.Write("{0}", result);
Archive(result, file);
}
///-------------------------------another class that is used--------------///
public class WatcherSettings
{
public IFileProcessor CreateProcessor()
{
return new SblFtpTransport(new Settings()
{
AchiveFolder = #"C:\Docs\place\Watcher\Archived",
FtpPort = "22",
FtpServer = "xxxxx.someplace.net",
FtpTargetPath = "/StudentBatchLoad_FW",
FtpUsername = "xxx",
Password = "xxxxxxx",
});
}
public string WatcherPath { get; set; }
}
As far as I can tell, you never call CreateProcessor(). And it appears you need to call that so the settings get created properly with the remote host, and that's why you get an error that you're trying to copy to local host. So change your code to call that.
But your code is extremely disjointed and hard to read. Spend some time cleaning it, and step through it with a debugger to see exactly what's happening.

Loop through set of files and check if it is comma delimited in C#

I need to loop through set of files and check if it is comma delimited or not in C#.I am very new to C#.Please help me with this.
Thanks in advance.
As someone already pointed out it is not going to be easy solution.
It could be very easy if every comma delimited file had a specified extension (for example: csv). If not the following algorigthm should work:
Retrieve all names (paths + names) of files in specified directory. If needed filter only those that may be of interest. Hint: take a look at System.IO.Directory and System.IO.File and System.IO.DirectoryInfo and System.IO.FileInfo
You have to examine every file, and check if it is comma delimited or not. This is going to be tricky part. You could build a regular expression, that will check each line of the file and and tell you if it is comma delimited or not.
Regular expressions are a bit hard to learn at the beginning but it should pay back after some time.
Here's a quick Console app that will take a directory, scan the directory for all files, then iterate through them and return a percentage of lines containing commas -vs- the total lines in the file. As has been pointed out, there are CSV libraries you can validate against. This is just a quick example to get you started.
To use this, create a new Console App project in Visual Studio and name it "TestStub" then copy and past this into the "Program.cs" file.
namespace TestStub
{
using System;
using System.IO;
using System.Text;
public class Program
{
private static char[] CSV = { ',', ',' };
private static bool csvFound = false;
/// <summary>
/// This is the console program entry point
/// </summary>
/// <param name="args">A list of any command-line args passed to this application when started</param>
public static void Main(string[] args)
{
// Change this to use args[0] if you like
string myInitialPath = #"C:\Temp";
string[] myListOfFiles;
try
{
myListOfFiles = EnumerateFiles(myInitialPath);
foreach (string file in myListOfFiles)
{
Console.WriteLine("\nFile {0} is comprised of {1}% CSV delimited lines.",
file,
ScanForCSV(file));
}
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(
"Error processing {0} for CSV content: {1} :: {2}",
myInitialPath,
ex.Message,
ex.InnerException.Message);
}
}
/// <summary>
/// Get a list of all files for the specified path
/// </summary>
/// <param name="path">Directory path</param>
/// <returns>String array of files (with full path)</returns>
public static string[] EnumerateFiles(string path)
{
string[] arrItems = new string[1];
try
{
arrItems = Directory.GetFiles(path);
return arrItems;
}
catch (Exception ex)
{
throw new System.IO.IOException("EnumerateFilesAndFolders() encountered an error:", ex);
}
}
/// <summary>
/// Determines if the supplied file has comma separated values
/// </summary>
/// <param name="filename">Path and filename</param>
/// <returns>Percentage of lines containing CSV elements -vs- those without</returns>
public static float ScanForCSV(string filename)
{
//
// NOTE: You should look into one of the many CSV libraries
// available. This method will not carefully scruitinize
// the file to see if there's a combination of delimeters or
// even if it's a plain-text (e.g. a newspaper article)
// It just looks for the presence of commas on multiple lines
// and calculates a percentage of them with and without
//
float totalLines = 0;
float linesCSV = 0;
try
{
using (StreamReader sReader = new StreamReader(filename))
{
int elements = 0;
string line = string.Empty;
string[] parsed = new string[1];
while (!sReader.EndOfStream)
{
++totalLines;
line = sReader.ReadLine();
parsed = line.Split(CSV);
elements = parsed.Length;
if (elements > 1)
{
++linesCSV;
}
}
}
}
catch (Exception ex)
{
throw new System.IO.IOException(string.Format("Problem accessing [{0}]: {1}", filename, ex.Message), ex);
}
return (float)((linesCSV / totalLines) * 100);
}
}
}
}

File extension restriction to .cor (only)

How can I restrict only .cor files to be added to the list.
The code bellow allows .corx, .corxx, .corxxx to be added to the list.
I only want .cor files. Is that possible?
private void btn_models_Click(object sender, EventArgs e)
{
DialogResult res = dlg_find_folder.ShowDialog();
if (res == DialogResult.OK)
{
tbx_models.Text = dlg_find_folder.SelectedPath;
populateChecklist(tbx_models.Text, "cor");
cbx_all.CheckState = System.Windows.Forms.CheckState.Checked;
}
}
/// <summary>
/// Function populates the models checklist based on the models found in the specified folder.
/// </summary>
/// <param name="directory">Directory in which to search for files</param>
/// <param name="extension">File extension given without period</param>
private void populateChecklist(String directory, String extension)
{
clb_run_list.Items.Clear();
System.Collections.IEnumerator enumerator;
String mdl_name;
try
{
enumerator = System.IO.Directory.GetFiles(directory, "*." + extension).GetEnumerator();
while (enumerator.MoveNext())
{
mdl_name = parse_file_name((String)enumerator.Current, directory, extension);
clb_run_list.Items.Add(mdl_name);
}
}
catch
{
//above code will fail if the initially specified directory does not exist
//MessageBox.Show("The specified directory does not exist. Please select a valid directory.");
}
return;
}
How about;
if (Path.GetExtension(mdl_name).Equals(".cor", StringComparison.OrdinalIgnoreCase))
clb_run_list.Items.Add(mdl_name);
Do a check for FileName.EndsWith(extension) before adding to your list?
This is an artifact of Windows support for old DOS 8.3 filenames. Files with an extension like .corxxx get mapped to a 8.3 name like Blah~1.cor. And will match your wildcard.
Nothing you can do but double-check the filename you get. Use Path.GetExtension()

Acessing a url from windows application

Does anyone know how to acess a url from a windows application?.
I have an address http://serverport/Page.I want to acess this page from my windows application.
Regards,
Harsh Suman
It's not clear what you want to do with the page.
If you want to display it on the form, you can use a WebBrowser control.
If you want to get the response and process it, use the System.Net.WebClient class.
If you want to download an HTML or any file you can use the WebClient class.
Example:
/// <summary>
/// Downloads a file from the given location
/// </summary>
/// <param name="url">Location of the file</param>
/// <param name="dest">The destination of the downloaded file</param>
/// <returns>False if there was an error, else True</returns>
public bool DownLoad(string url, string dest)
{
WebClient client = new WebClient();
try
{
//Downloads the file from the given url to the given destination
client.DownloadFile(url, dest);
return true;
}
catch (WebException)
{
// Handle exception
return false;
}
catch (System.Security.SecurityException)
{
// Handle exception
return false;
}
catch (Exception)
{
// Handle exception
return false;
}
}
I'm not sure what you're asking for,so I just give the answer to yet another way to interpret the question.
If you simply want to launch the default browser (to display a local or online html manual etc.), in windows (and probably similar in other OS'es) you can use some kind of "execute interface" to execute a correctly formatted url as the command, this will usually launch the default browser:
According to this page this code should launch a browser:
string targeturl= "http://stackoverflow.com";
try
{
System.Diagnostics.Process.Start(targeturl);
}
catch
(
System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
(It looks pretty ugly with magic numbers for error codes, though...)

Categories

Resources