2014-02-21 38 views
0

Windows应用程序如何从客户端使用C#4.0如何从客户端使用C#4.0

Windows应用程序读取文本文件在服务器和客户端应用程序中的文本框显示相同的阅读文本文件在服务器

能够在本地读取文本并在文本框中打印相同

但是来自服务器怎么办?

下面

是我尝试了本地文件

string line; 
StringBuilder sb = new StringBuilder(); 
int counter = 0; 

using (StreamReader file = new StreamReader(path)) 
{ 
    while ((line = file.ReadLine()) != null) 
     { 
     if (line.Contains(searchstring)) 
      { 
       if (line.Contains(searchfromdate)) //|| line.Contains(searchtodate)) 
        { 
         sb.AppendLine(line.ToString()); 
         counter++; 
        } 
      } 
     } 
} 

ResultTextBox.Text = sb.ToString(); 
CountLabel.Text = counter.ToString(); 

回答

2

访问服务器上的文件的代码,你需要两件事情

  • 确保您使用的是AA用户是HASS权限访问文件

  • 将服务器地址路径设置为\ servername1 \ Folder \ file.txt

所以在使用你的代码,你就必须有像

string line; 
string path = @"\\server1\TextFolder\Text.txt"; 
StringBuilder sb = new StringBuilder(); 
int counter = 0; 

using (StreamReader file = new StreamReader(path)) 
{ 
    while ((line = file.ReadLine()) != null) 
     { 
     if (line.Contains(searchstring)) 
      { 
       if (line.Contains(searchfromdate)) //|| line.Contains(searchtodate)) 
        { 
         sb.AppendLine(line.ToString()); 
         counter++; 
        } 
      } 
     } 
} 

ResultTextBox.Text = sb.ToString(); 
CountLabel.Text = counter.ToString(); 
+0

感谢Martyn Weber你的回答有效!!!!! – kumartyr

1

需要Share文件夹中的服务器计算机,并提供Read权限谁想要从remotly访问它的用户。

2.获取服务器计算机的IPAddressHostname,以便您可以访问共享文件夹。

现在准备如下文件路径:

例如:如果ServerName是MyServer123和文件夹名是MyFolder FileName是myFile.txt

你的路径应该是"\\MyServer123\MyFolder\MyFile.txt"

完整代码:

StringBuilder sb = new StringBuilder(); 
int counter = 0; 
String [email protected]"\\MyServer123\MyFolder\MyFile.txt"; 
foreach(var line in File.ReadLines(path)) 
{ 
    if (line.Contains(searchstring) && (line.Contains(searchfromdate))) 
    { 
    sb.AppendLine(line); 
    counter++; 
    } 
} 
ResultTextBox.Text = sb.ToString(); 
CountLabel.Text = counter.ToString(); 
+0

感谢Sudhakar你的回答也有效!!!!! – kumartyr