在网络路径目录上创建文件时,C#访问被拒绝



编辑:因此,我现在已经获得了该网络文件夹的服务帐户"docwriter"和密码"password"。我已经创建了一个ConnectionToNetworkFolder类,并尝试过多次尝试使用它。但仍然没有成功。我在尝试将pdf文件保存在网络的路径目录中时遇到问题,但保存到本地文件夹时工作正常。见帖子底部,我包括了我的课程和我的页面


原始帖子:我以前试过几个代码。我还更改了网络文件夹的安全权限,添加了"网络服务"(从网上(,有权修改,阅读&执行、列出文件夹内容、读取、写入(我需要特殊权限还是完全控制?(。我做错了什么?

***本地文件夹:成功

Telerik.Reporting.Processing.ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
string documentName = ProductionInstructionPrintPage.plantCode + ProductionInstructionPrintPage.machine + ProductionInstructionPrintPage.ordNum;
string filePath = @"C:UsersLilyDesktopSavingPdfTesting";
string fileName = documentName + "." + result.Extension;
string full_path = filePath + fileName;
//writes the pdf to disk
using (FileStream fs = new FileStream(full_path, FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}

***网络尝试1:失败-拒绝访问

Telerik.Reporting.Processing.ReportProcessor reportProcessor = new ReportProcessor();
RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
string documentName = ProductionInstructionPrintPage.plantCode + ProductionInstructionPrintPage.machine + ProductionInstructionPrintPage.ordNum;
string filePath = @"\testing.company.comreports";
string fileName = documentName + "." + result.Extension;
string full_path = filePath + fileName;
//writes the pdf to disk
using (FileStream fs = new FileStream(full_path, FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}

************************编辑**********************************

这是我的ConnectionToNetworkFolder类:

class ConnectionToNetworkFolder : IDisposable
{
string _networkName;
public ConnectionToNetworkFolder(string networkName, NetworkCredential credentials)
{
_networkName = networkName;

var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}{1}", credentials.Domain, credentials.UserName);
var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);
if (result != 0)
{
throw new Win32Exception(result, "Error connecting to remote share");
}
}
~ConnectionToNetworkFolder()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}

我的页面:声明凭证变量:

NetworkCredential credentials = new NetworkCredential(@"docwriter", "password");

点击事件功能:

private void apprBtn_Click(object sender, EventArgs e)
{
string documentName = Mainpage.apprDate;
string filePath = @"\testing.company.comreports";
string fileName = documentName + "." + result.Extension;
string full_path = filePath + fileName;

using (new ConnectionToNetworkFolder(filePath, credentials))
{
//writes the pdf to disk
using (FileStream fs = new FileStream(full_path, FileMode.Create))
{
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
}
}
}

检查共享的权限。正如你所说,你添加了"网络服务"的权限,但运行你的应用程序的用户是什么?

我的猜测是您需要模拟服务器上的文件访问。您可能没有足够的共享权限,因为您正在"写入"网络。

下面是一个关于如何模拟文件/文件夹访问的示例。它清楚地解释了你需要做什么。
代码项目UNC访问

相关内容

  • 没有找到相关文章

最新更新