Reputation: 19269
In Windows you can add a FTP site as a named Network Location using the "Add Network Location Wizard". For instance, a user can add a location called "MyFtp".
In .Net, how can I access (list, read and write) files in that location? Does Windows abstract away the implementation (WebDAV, FTP or else) and make it look like a local folder to my .Net program? If that's the case, how do I specify the path
parameter in File.WriteAllText(path, content)
? If not, how can I access the files?
Upvotes: 2
Views: 5537
Reputation: 45172
The MyFtp shortcut in the Network Locations is a shortcut to the FTP Folders shell namespace extension. If you want to use it, you would have to bind to the shortcut target (via the shell namespace) and then navigate via methods like IShellFolder::BindToObject or IShellItem::BindToHandler. This is very advanced stuff and I don't think there is anything built into C# to make it easier. Here are some references to get you started.
Upvotes: 2
Reputation: 190976
No, Windows only handles that in Explorer. (They might have removed this in newer versions of Windows.) You will have to use some built in classes or implement FTP, WebDav and any other protocol yourself.
Upvotes: 4
Reputation: 9039
You can try this to read/write the content of a file at the network location
//to read a file
string fileContent = System.IO.File.ReadAllText(@"\\MyNetworkPath\ABC\\testfile1.txt");
//and to write a file
string content = "123456";
System.IO.File.WriteAllText(@"\\MyNetworkPath\ABC\\testfile1.txt",content);
But you need to provide read/write permissions for network path to the principal on which the application is running.
Upvotes: 1
Reputation: 52290
you can use the FtpWebRequest-Class
here is some sample-code (from MSDN):
public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}
Upvotes: 0