如何使用C#4.0从Windows应用程序中的客户端读取服务器中的文本文件

时间:2021-11-14 19:00:01

how to read text file in server from client in windows application using C# 4.0

如何使用C#4.0从Windows应用程序中的客户端读取服务器中的文本文件

and display the same in textbox in the client application

并在客户端应用程序的文本框中显示相同内容

able to read the text locally and print the same in textbox

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

but from server how to do?

但是从服务器怎么办?

below is the code i tried for local file

下面是我为本地文件尝试的代码

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 个解决方案

#1


2  

To access files on a server you need 2 things

要访问服务器上的文件,您需要两件事

  • Make sure your are using aa user that hass permission to access the file

    确保您使用的是具有访问该文件权限的用户

  • Set the server address path as \servername1\Folder\file.txt

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

so using your code you would have to have something like

所以使用你的代码你必须有类似的东西

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();

#2


1  

1. you need to Share the folder in server machine and provide Read permission to the users who want to access it from remotly.

1.您需要在服务器计算机*享该文件夹,并为希望从远程访问它的用户提供“读取”权限。

2. Get the IPAddress or Hostname of the Server machine so that you can access the shared folders.

2.获取服务器计算机的IPAddress或主机名,以便可以访问共享文件夹。

3. Now prepare the filepath as below:

3.现在准备文件路径如下:

Example: if ServerName is MyServer123 and FolderName is MyFolder FileName is myFile.txt

示例:如果ServerName是MyServer123而FolderName是MyFolder,则FileName是myFile.txt

your path should be "\\MyServer123\MyFolder\MyFile.txt"

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

Complete Code:

完整代码:

StringBuilder sb = new StringBuilder();
int counter = 0;
String path=@"\\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();

#1


2  

To access files on a server you need 2 things

要访问服务器上的文件,您需要两件事

  • Make sure your are using aa user that hass permission to access the file

    确保您使用的是具有访问该文件权限的用户

  • Set the server address path as \servername1\Folder\file.txt

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

so using your code you would have to have something like

所以使用你的代码你必须有类似的东西

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();

#2


1  

1. you need to Share the folder in server machine and provide Read permission to the users who want to access it from remotly.

1.您需要在服务器计算机*享该文件夹,并为希望从远程访问它的用户提供“读取”权限。

2. Get the IPAddress or Hostname of the Server machine so that you can access the shared folders.

2.获取服务器计算机的IPAddress或主机名,以便可以访问共享文件夹。

3. Now prepare the filepath as below:

3.现在准备文件路径如下:

Example: if ServerName is MyServer123 and FolderName is MyFolder FileName is myFile.txt

示例:如果ServerName是MyServer123而FolderName是MyFolder,则FileName是myFile.txt

your path should be "\\MyServer123\MyFolder\MyFile.txt"

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

Complete Code:

完整代码:

StringBuilder sb = new StringBuilder();
int counter = 0;
String path=@"\\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();