读取C#中的隐藏共享

发布于 2024-07-05 11:35:40 字数 168 浏览 9 评论 0原文

因此,我有一个小型 C# 应用程序,需要定期检查网络上多台计算机上的目录内容。 我以为我可以将 \hostname\C$ 读取为目录路径,但对于普通的 Directory 类,似乎没有一种方法可以对其他服务器进行身份验证,以便您可以访问隐藏的共享。 我确信有一种简单的方法可以做到这一点,但我忽略了,但目前我有点困惑。

So I have a small C# app that needs to periodically check the contents of directories on multiple machines on the network. I thought I could just read \hostname\C$ as a directory path, but with the normal Directory class there doesn't seem to be a way to authenticate against the other servers so you can access the hidden share.
I'm sure there's an easy way to do this that I've overlooked, but at the moment I'm a bit stumpted.

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

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

发布评论

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

评论(3

撕心裂肺的伤痛 2024-07-12 11:35:40

来自 http://bytes.com/forum/thread689145.html

所有进程都在一个上下文中运行
登录的用户帐户。 如果你想
在另一台计算机上打开文件
应用程序必须运行在
具有权限的用户的上下文
打开该机器上的文件。 你可以
通过模拟来做到这一点。

最简单的方法似乎是授予当前用户在其他计算机上的适当权限。

From http://bytes.com/forum/thread689145.html:

All processes run in the context of a
logged-in user account. If you want to
open a file on another computer, your
application must be running in the
context of a user that has permissions
to open files on that machine. You can
do this with Impersonation.

The easiest way seems to be to give the current user appropriate rights on the other machines.

太阳哥哥 2024-07-12 11:35:40

要对运行进程的用户没有权限的共享进行身份验证(管理共享通常是这种情况),请尝试运行 net use 命令:

net use SERVERNAME\IPC$ /user:USERNAME PASSWORD

尝试在实际尝试访问该进程的代码之前在单独的进程中运行此命令共享,例如:

ProcessStartInfo psi = new ProcessStartInfo(
    "net", "use " + SERVERNAME + @"\IPC$ /user:" + USERNAME + " " + PASSWORD);
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
p.Close();
// The code to access the share follows...

如果不适合为运行进程的用户帐户授予共享权限,则这很有用,例如对于最终用户应用程序需要访问用户自己应该访问的共享上的数据的安全模型无法直接访问。

To authenticate with a share to which the user running the process does not have permission (which is often the case for administrative shares), try running the net use command:

net use SERVERNAME\IPC$ /user:USERNAME PASSWORD

Try running this in a separate process before the code which actually tries to access the share, e.g.:

ProcessStartInfo psi = new ProcessStartInfo(
    "net", "use " + SERVERNAME + @"\IPC$ /user:" + USERNAME + " " + PASSWORD);
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.WaitForExit();
p.Close();
// The code to access the share follows...

This is useful if it is not appropriate to give permission to the share for the user account running the process, e.g. for a security model where an end-user application needs to access data on a share to which the user herself should not have direct access.

猫瑾少女 2024-07-12 11:35:40

您是否正在寻找一种在运行时设置当前用户的方法?

如果没有,只要运行该进程的用户可以访问这些机器,这对您有用:

DirectoryInfo di = new DirectoryInfo(@"\\machineName\c$\temp");

FileInfo[] files = di.GetFiles();

foreach (FileInfo f in files)
{
    Debug.WriteLine(f.Name);
}

Are you looking for a way to set the current user at run-time?

If not, as long as the user running the process has access to those machines, this will work for you:

DirectoryInfo di = new DirectoryInfo(@"\\machineName\c$\temp");

FileInfo[] files = di.GetFiles();

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