在网络路径目录上创建文件时,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:\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,result.DocumentBytes.Length);
            }

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

            Telerik.Reporting.Processing.ReportProcessor reportProcessor = new ReportProcessor();
            RenderingResult result = reportProcessor.RenderReport("PDF",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,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,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,result.DocumentBytes.Length);
                    }

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

检查共享的权限。如您所述,您已向“网络服务”添加了权限,但是运行您的应用程序的用户是什么?

,

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

这里是有关如何模拟文件/文件夹访问的示例。它清楚地说明了您需要做什么。
Code Project UNC Access

本文链接:https://www.f2er.com/3122840.html

大家都在问