如何使用 C# 代码访问共享网络驱动器文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10736790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Access Shared Network drive File using C# code
提问by kuldeep verma
Manually I Have Mapped Network Drive Y://To My System .Drive is having Manny Folders each Containg Single XMl File having Same as Folder .
我手动将网络驱动器Y://映射 到我的系统。驱动器有 Manny 文件夹,每个文件夹都包含与文件夹相同的单个 XMl 文件。
Here I am Trying to Read Xml File From Network Location . But It is Giving Exception Directory Not Found. Below Code I am Using For that .
在这里,我试图从网络位置读取 Xml 文件。但它给出了未找到的异常目录。下面是我为此使用的代码。
Fname = txtwbs.Text;
DirectoryInfo objDir = new DirectoryInfo("Y:\");
\Y:\
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
_xmlpath = objDir + "\" + Fname + "\" + Fname + ".xml";
if (File.Exists(_xmlpath ))
{
reader(_xmlpath);
}
}
Here Fname is Folder Name Also Xml Name .Whatever User Will Enter the Name of File .
这里 Fname 是文件夹名称也是 Xml 名称。无论用户将输入文件的名称。
回答by Mr.GT
Use this API. http://www.codeproject.com/Articles/6847/Map-Network-Drive-API
使用这个 API。http://www.codeproject.com/Articles/6847/Map-Network-Drive-API
It the only way i have managed to do it in code. Just modify the class, so it fits to you project.
这是我在代码中设法做到的唯一方法。只需修改类,使其适合您的项目。
回答by MichelZ
Ideally, you should use the UNC Pathto access the files. \\server\share\path\to\file
理想情况下,您应该使用UNC 路径来访问文件。\\server\share\path\to\file
回答by Dave Simione
You have this post tagged as both asp.net and asp-classic. From your code example, I'm guessing asp-classic doesn't apply.
您将此帖子标记为 asp.net 和 asp-classic。从您的代码示例中,我猜 asp-classic 不适用。
If you are running in ASP.Net, the system wouldn't know about the mapped drive you've created. You should use the UNC path instead. If you aren't running the site with Windows Authentication, you'll also need to impersonate someone who has access to the share, as your anonymous user most likely will receive "Access Denied" errors.
如果您在 ASP.Net 中运行,系统将不知道您创建的映射驱动器。您应该改用 UNC 路径。如果您没有使用 Windows 身份验证运行该站点,您还需要冒充有权访问共享的人,因为您的匿名用户很可能会收到“拒绝访问”错误。
Finally, I don't believe you need the DirectoryInfo call - use Path.Combine()
最后,我不相信你需要 DirectoryInfo 调用 - 使用 Path.Combine()
回答by willDaBeast
Use a UNC path rather than a network drive and make sure the user running you application has permissions to access the folder and it's contents. "File not found" can mean wrong permissions or the path is just wrong.
使用 UNC 路径而不是网络驱动器,并确保运行您的应用程序的用户有权访问该文件夹及其内容。“找不到文件”可能意味着权限错误或路径错误。
回答by Esen
Identity impersonate has done the trick for me
身份冒充为我成功了
......
<system.web>
<identity impersonate="true" userName="yourdomain\yourusername"
password="yourpassword" />
......
......
回答by boskop
Grab this code: http://pastebin.com/RZnydz4Z
获取此代码:http: //pastebin.com/RZnydz4Z
Then on the Application_Start of your global.asax put this:
然后在你的 global.asax 的 Application_Start 上放这个:
protected void Application_Start(object sender, EventArgs e)
{
Utilities.Network.NetworkDrive nd = new Utilities.Network.NetworkDrive();
nd.MapNetworkDrive(@"\server\path", "Z:", "myuser", "mypwd");
}
Then just use like a normal network drive, like this:
然后就像普通的网络驱动器一样使用,如下所示:
File.ReadAllText(@"Z:\myfile.txt");

