简体   繁体   English

如何读写Windows的“网络位置”

[英]How to read and write to a Windows “Network Location”

In Windows you can add a FTP site as a named Network Location using the "Add Network Location Wizard". 在Windows中,可以使用“添加网络位置向导”将FTP站点添加为命名的网络位置。 For instance, a user can add a location called "MyFtp". 例如,用户可以添加一个名为“ MyFtp”的位置。

In .Net, how can I access (list, read and write) files in that location? 在.Net中,我如何访问(列出,读取和写入)该位置的文件? Does Windows abstract away the implementation (WebDAV, FTP or else) and make it look like a local folder to my .Net program? Windows是否将实现(WebDAV,FTP或其他)抽象化,并使其看起来像.Net程序的本地文件夹? If that's the case, how do I specify the path parameter in File.WriteAllText(path, content) ? 如果是这样,如何在File.WriteAllText(path, content)指定path参数? If not, how can I access the files? 如果没有,如何访问文件?

No, Windows only handles that in Explorer. 不,Windows仅在资源管理器中处理。 (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. (他们可能已在Windows的较新版本 中将其 删除。) 您将必须使用一些内置类或自己实现FTP,WebDav和任何其他协议。

The MyFtp shortcut in the Network Locations is a shortcut to the FTP Folders shell namespace extension. 网络位置中的MyFtp快捷方式是FTP文件夹外壳名称空间扩展的快捷方式。 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. 如果要使用它,则必须绑定到快捷方式目标(通过外壳名称空间),然后通过IShellFolder :: BindToObject或IShellItem :: BindToHandler之类的方法进行导航。 This is very advanced stuff and I don't think there is anything built into C# to make it easier. 这是非常高级的东西,我认为C#内置没有任何东西可以使其变得更容易。 Here are some references to get you started. 以下是一些入门指南。

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. 但是,您需要为运行应用程序的主体的网络路径提供读/写权限。

you can use the FtpWebRequest -Class 您可以使用FtpWebRequest -Class

here is some sample-code (from MSDN): 这是一些示例代码(来自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","janeDoe@contoso.com");
    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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM