2

Is there something similar to C# connection string for FTP connection? Something I can write as a full string and manage easily like:

ftp://username:password@domain:port

I explain more what I'm trying to do. I'm writing a small console application that should, let's keep this example simple, download all file from a FTP. I would like to use a command line query similar to the one we use for nuget package for example. A good example is this one:

Update-database -connectionString "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Local;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True" -ConnectionProviderName "System.Data.SqlClient" -verbose

So for my FTP it sill be something similar to

Download-Ftp -ftp "ftp://usr202:P@ssw0rd@example.com:21"

But also with more details like

Download-Ftp -ftp "ftp://usr202:P@ssw0rd@example.com:21;Sftp=false;AnyOtherFtpParameter=value"

Of course I can do it myself. My question here and now is. Is there already something, some standard, a better way to manage this or no nothing?

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • You just need to somehow escape `@` in your `P@ssw0rd`. You may use a [UriBuilder](https://learn.microsoft.com/ru-ru/dotnet/api/system.uribuilder.-ctor?view=netframework-4.8#System_UriBuilder__ctor_System_String_System_String_System_Int32_) for this: `new UriBuilder("ftp", "example.com", 21) { UserName = "usr202", Password = Uri.EscapeDataString("P@ssw0rd") }.Uri` – vasily.sib Jul 04 '19 at 03:22
  • For SFTP, use `sftp://`, not separate `Sftp` parameter of `ftp://` URL. – Martin Prikryl Jul 04 '19 at 05:26

1 Answers1

2

For a well-formed FTP URL you can use the standard System.Uri class.

Your second example with extra options is not a well-formed URL and I would recommend against implementing something odd like that.

Since you're writing a command-line application, I would instead suggest implementing command line options (i.e. switches) for those extra settings.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Interesting link. I'm still reading it but I have just one more question about this, for confirmation. An Uri can represent a file or a folder, right? (and many other things I guess) – Bastien Vandamme Jul 04 '19 at 03:15
  • Yes. For simplicity just think of it as a URL, just like you see in your address bar right now. – Jonathon Reinhart Jul 04 '19 at 03:20
  • 1
    +1 – Though note that URL can contain additional parameters – While it's rarely, if ever, used. See [Parameters in Path Segments of URL](https://stackoverflow.com/q/40440004/850848). – Martin Prikryl Jul 04 '19 at 05:27
  • @Martin Very interesting, thanks! I guess what I was trying to say was that what the OP has presented isn't part of a standard FTP URL, so he shouldn't try to use something non-standard. – Jonathon Reinhart Jul 04 '19 at 13:54