如何从客户端到服务器访问文件

本文关键字:服务器 访问 文件 客户端 | 更新日期: 2023-09-27 17:49:39

private void BtnInsert_Click(object sender, EventArgs e)
{
        {

            string strDir = "http://200.100.0.50/chandu/abc.txt";
            if (!File.Exists(strDir))
            {
                File.Create(strDir);
            }
        }
}

我正在插入记录的形式,将存储在服务器的驱动器C:但得到URI格式不是在正确的格式错误。

如何从客户端到服务器访问文件

这是因为您需要使用UNC路径,如:

''200.100.0.50'chandu'abc.txt

使用WebRequest,那么您将获得发送HTTP HEAD请求的能力。当您发出请求时,您应该得到一个错误(如果文件丢失),或者一个具有有效ContentLength属性的WebResponse

WebRequest request = WebRequest.Create(new Uri("http://www.example.com/"));
request.Method = "HEAD";
WebResponse response = request.GetResponse();
Console.WriteLine("{0} {1}", response.ContentLength, response.ContentType);

如果目标支持webdav,您可以将其映射为网络驱动器或直接访问它,但您仍然需要执行'200.100.0.5'whatever

用'代替/

在一个稍微不相关的说明(希望我理解你的问题):

您应该创建WebClient并使用DownloadString函数。如果文件存在,该函数将抛出404异常。

WebClient client = new WebClient();
client.DownloadString("www.google.com"); // Will work
client.DownloadString("www.asdfawioeufje.com/awefoasdfasdf"); // 404 error

正如你所看到的,这可能是我认为你想要做的更好的方法。