编辑: 因此,我现在已经获得了该网络文件夹的服务帐户“ 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:\Users\Lily\Desktop\SavingPdfTesting";
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.com\reports\";
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.com\reports\";
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);
}
}
}
答案 0 :(得分:0)
检查共享的权限。如您所述,您已向“网络服务”添加了权限,但是运行您的应用程序的用户是什么?
答案 1 :(得分:0)
我的猜测是您需要模拟服务器上的文件访问。您可能没有足够的共享权限,因为您正在“写入”网络。
这里是有关如何模拟文件/文件夹访问的示例。它清楚地说明了您需要做什么。
Code Project UNC Access