C# 通过 Web 服务访问文件夹/文件

发布于 2024-08-16 03:12:34 字数 566 浏览 2 评论 0原文

如何让网络服务读取/浏览文件夹内容?

例如这种类型的代码:

    FolderBrowserDialog folderBrowser;
    folderBrowser = new System.Windows.Forms.FolderBrowserDialog();

    folderBrowser.Description = "...";
    folderBrowser.ShowNewFolderButton = false;
    folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;

当我构建解决方案时,我收到此错误...

类型或命名空间名称 “FolderBrowserDialog”不能 找到了(您是否缺少使用 指令还是程序集引用?)

我知道尝试在 Web 服务中使用对话框没有多大意义,但我还能怎么做呢?

我的网络服务收到一个字符串,然后我想浏览文件夹中包含该字符串的文件。

How can I have a webservice read/browse a folder content ?

For instance this type of code:

    FolderBrowserDialog folderBrowser;
    folderBrowser = new System.Windows.Forms.FolderBrowserDialog();

    folderBrowser.Description = "...";
    folderBrowser.ShowNewFolderButton = false;
    folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;

When I build the solution I get this error...

The type or namespace name
'FolderBrowserDialog' could not be
found (are you missing a using
directive or an assembly reference?)

I know it doesn't make a lot of sense trying to use a dialog in a webservice but how else can I do it?

My webservice receives a string and then I want to browse for files that contain that string in a folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流星番茄 2024-08-23 03:12:34

您需要使用 System.IO 命名空间来导航到您的文件系统;正如您所指出的,尝试在 Web 服务调用上显示对话框是没有意义的。

You'll need to use System.IO namespace to navigate into your filesystem; as you noted, doesn't make sense trying to display a dialog on a webservice call.

揪着可爱 2024-08-23 03:12:34

查看 System.IO.Directory.GetFiles() 方法。显示FolderBrowser 对话框自然只能与胖客户端交互式WinForms 应用程序一起使用。

Have a look at the System.IO.Directory.GetFiles() method. Displaying the FolderBrowser dialog can naturally only be used with thick client interactive WinForms apps.

驱逐舰岛风号 2024-08-23 03:12:34

使用 StreamReader 读取文本文件:

StreamReader reader = File.OpenText(filename);

string contents = reader.ReadToEnd();

reader.Close();

列出文件夹中的文件:

 DirectoryInfo di = new DirectoryInfo(fullPathToFolder);
 FileInfo[] fileList = di.GetFiles("*.aspx");

 foreach(FileInfo fi in fileList)
 {
     // do something with fi.Name
 }

Use a StreamReader to read a text file:

StreamReader reader = File.OpenText(filename);

string contents = reader.ReadToEnd();

reader.Close();

To list files in a folder:

 DirectoryInfo di = new DirectoryInfo(fullPathToFolder);
 FileInfo[] fileList = di.GetFiles("*.aspx");

 foreach(FileInfo fi in fileList)
 {
     // do something with fi.Name
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文