[英]C# Access to the path is denied exception creating a file in Documents
[英]C# Access denied when creating file on network path directory
編輯:所以我現在已經獲得了該網絡文件夾的服務帳戶“docwriter”和密碼“password”。 我創建了一個 class ConnectionToNetworkFolder 並嘗試了幾次嘗試使用它。 但仍然沒有成功。 我在嘗試將 pdf 文件保存在我的網絡路徑目錄中時遇到問題,但在保存到本地文件夾時工作正常。 見帖子底部,我包括了我的 class 和我的頁面
原帖:我已經嘗試過之前顯示的幾個代碼。 我還更改了網絡文件夾的安全權限,添加了“網絡服務”(從在線),可以修改、讀取和執行、列出文件夾內容、讀取、寫入(我需要特殊權限還是完全控制?)。 我究竟做錯了什么?
***本地文件夾:成功
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:
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");
function 點擊事件:
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);
}
}
}
檢查共享的權限。 正如您所說,您向“網絡服務”添加了權限,但是運行您的應用程序的用戶是什么?
我的猜測是您需要在服務器上模擬文件訪問。 在向網絡“寫入”時,您可能沒有足夠的共享權限。
這是一個關於如何模擬文件/文件夾訪問的示例。 它清楚地解釋了您需要做什么。
代碼項目 UNC 訪問
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.