如何使用cocoa/obj-c从挂载的目录中获取服务器主机名? [英] How do I get the server hostname from a mounted directory with cocoa/obj-c?

查看:78
本文介绍了如何使用cocoa/obj-c从挂载的目录中获取服务器主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当我使用程序打开文件时,可以通过单击NSOpenPanel侧栏中的服务器名称来选择服务器上的文件,然后选择文件.没问题,只要挂载了共享目录,使用该文件就可以正常工作.我得到一个类似"/Volumes/SHARENAME/filename.bla"的路径.

我的问题是如何获取其来源计算机的服务器主机名.例如,如果我在NSOpenPanel中单击共享"下名为SERVERNAME的设备,如何从"/Volumes/SHARENAME/filename.bla"获得SERVERNAME.

我看了很多文档,但无法找到解决该问题的方法.

任何对此的帮助将不胜感激.谢谢.

解决方案

这不是使用Objective-C的方法,但有时使用popen(..)可以让您获取可以从unix命令解析的信息.

示例

#include <stdio.h>
#include <string.h>

int main() {
  FILE *fp = popen("df", "r"); // see man page for df
  if (fp) {
    char line[4096];
    while (line == fgets(line, 4096, fp)) {
      if (strstr(line, "/Volumes/SHARENAME")) { // You need the mount point
        char host[256];
        sscanf(line, "%s", host);
        printf("Connected: %s\n", host);
      }
    }
    pclose(fp);
  }
  return 0;
}

Currently when I open a file with my program I can select files on a server by clicking on the server name in the sidebar in an NSOpenPanel and then selecting the file. No problem, this works fine for using the file as long as the shared directory is mounted. I get a path like "/Volumes/SHARENAME/filename.bla".

My question is how do I get the server hostname of the computer it came from. For instance, if I clicked on the device with name SERVERNAME under "Shared" in the NSOpenPanel how do I get SERVERNAME from "/Volumes/SHARENAME/filename.bla".

I have looked at quite a bit of documentation and have been unable to find a solution for this problem.

Any help toward this will be greatly appreciated. Thank you.

解决方案

This is not an Objective-C way of doing this but sometimes using popen(..) can get let you grab information you can parse from a unix command.

Example

#include <stdio.h>
#include <string.h>

int main() {
  FILE *fp = popen("df", "r"); // see man page for df
  if (fp) {
    char line[4096];
    while (line == fgets(line, 4096, fp)) {
      if (strstr(line, "/Volumes/SHARENAME")) { // You need the mount point
        char host[256];
        sscanf(line, "%s", host);
        printf("Connected: %s\n", host);
      }
    }
    pclose(fp);
  }
  return 0;
}

这篇关于如何使用cocoa/obj-c从挂载的目录中获取服务器主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆