2012-01-27 12 views
2

リモートサーバーに接続し、テキストファイルを読み取ってコンソールに表示しようとしています。リモートサーバーには、アクセスにユーザー名とパスワードが必要です。私はあなたに、これを行う最善の方法が何であるか尋ねたいと思います。C#フォームアプリケーションを使用してリモートサーバーに接続しようとしています

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Runtime.InteropServices; 
using System.IO; 
using Microsoft.Win32.SafeHandles; 

namespace WebclientTest 
{ 


class Server 
{ 
    [DllImport("kernel32")] 
    static extern bool AllocConsole(); 

    WebClient client = new WebClient(); 

    private string hostName; 
    private string userName; 
    private string password; 

    //Constructor gets host username and password 
    public Server(string _hostName, string _userName, string _password) 
    { 
     hostName = _hostName; 
     userName = _userName; 
     password = _password; 
    } 

    public void Connect() 
    { 
     AllocConsole(); 
     //Console.WriteLine("HelloWorld"); 
     //Console.ReadLine(); 
     Uri uri = new Uri(hostName); 
     Console.WriteLine(uri.Host.ToString()); 
     string fileLocation = uri.Host+"\someDirectory.textfile.txt"; 
     StreamReader strRead = new StreamReader(fileLocation); 
     Console.Write(strRead.ReadLine()); 
    } 
} 

}

+0

どのような認証タイプですか?基本認証?フォーム? –

答えて

0

ないことが最善の方法ですが、私はドライブをマッピングして、ファイルにアクセスするためにマップされたドライブ文字を使用して、類似した何かをやったかどうかわから:

private void mapDrive(String strDrive, String strLocation, string strUser, string strPassword) 
    { 
     System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
     proc.EnableRaisingEvents = false; 
     proc.StartInfo.FileName = "net"; 
     proc.StartInfo.Arguments = "use " + @strDrive + " " + @strLocation + " " + @" /USER:" + @strUser + " " + @strPassword; 
     proc.Start(); 
     proc.WaitForExit(); 
    } 

完了したら、マッピングを削除してください:

private void unmapDrive(String strDrive) 
    { 
     System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
     proc.EnableRaisingEvents = false; 
     proc.StartInfo.FileName = "net"; 
     proc.StartInfo.Arguments = "use " + @strDrive + @" /delete /yes"; 
     proc.Start(); 
     proc.WaitForExit(); 
    } 
関連する問題