2012-12-28 98 views
0
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格式不正確的格式錯誤。如何從客戶端訪問一個文件到服務器

回答

2

那是因爲你需要使用UNC路徑,如:如果目標支持WebDAV的

\\200.100.0.50\chandu\abc.txt 
+0

的 「http:\\ 200.100.0.50 \ chandu \的abc.txt」 我給了這樣再次無法識別逃生誤差正在添加 – user1918410

+0

@ user1918410嘗試'字符串strDir = @「\\ 200.100.0.50 \ chandu \ ABC .TXT「'。你沒有正確地逃避反斜槓。 – Lander

+0

URI格式不受支持 – user1918410

0

你可以把它映射網絡驅動器,或直接訪問它,但你仍然需要做\ 200.100。 0.5 \無論

0

使用\而不是/

在一個稍微不相關的說明(希望我理解你的問題):

您應該創建一個WebClient並使用DownloadString函數。如果文件存在,該函數將拋出404異常。

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

正如你所看到的,這可能是做什麼我想你想要做的更好的方法。

+0

對不起,我使用Windows感謝您的答案 – user1918410

+0

爲什麼這不工作,如果你使用Windows?我很困惑。 – AVP

1

使用WebRequest,那麼你就有能力發送HTTP HEAD請求。當您發出請求時,您應該得到一個錯誤(如果文件丟失)或WebResponse與有效的ContentLength屬性。

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); 
+0

我正在使用Windows應用程序,上面的代碼將在Windows中工作 – user1918410

相關問題