【问题标题】:c# reading csv file System.Security.SecurityExceptionc# 读取csv文件 System.Security.SecurityException
【发布时间】:2017-10-19 06:58:31
【问题描述】:

我需要在我的网络服务中读取一个 csv 文件,

这是我目前的代码:

[WebMethod]
    public List<string> getIdentifiants()
    {
        List<string> listA = new List<string>();
        List<string> listB = new List<string>();
        using (var fs = File.OpenRead(@"C:\Users\stag01\Desktop\identifiants.csv"))
        using (var reader = new StreamReader(fs))
        {

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(';');

                listA.Add(values[0]);
                listB.Add(values[1]);
            }
        }

        return listA;

    }

但是当我尝试运行它时,我得到了这个错误:

System.Security.SecurityException: Échec de la demande d'autorisation de type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'。
à System.Security.CodeAccessSecurityEngine.Check(对象需求, StackCrawlMark& stackMark, Boolean isPermSet)
à System.Security.CodeAccessPermission.Demand()
à System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
à System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
à System.IO.File.OpenRead(字符串路径)
à WebApplication1.WS_stage_2017.getIdentifiants() dans \Nas-server\User Folder\stag01\Mes documents\Visual Studio 2017\Projects\WebApplication1\WebApplication1\WS_stage_2017.asmx.cs:ligne 31

谁能帮帮我?

【问题讨论】:

  • 您的应用程序以有限的权限运行,您在测试时应该拥有完整的管理员帐户

标签: c# csv securityexception


【解决方案1】:

您需要读取权限。如果在本地调试,请以管理员身份运行 visual stuio。如果正在运行服务器,您需要在 iss 上设置您的服务应用程序池的读取权限。

此代码也错误。只能执行一个用户。 2个客户端不能同时打开这个文件。你需要使用锁来解决这个问题。

        [WebMethod]
    public List<string> getIdentifiants() {
        List<string> listA = new List<string>();
        List<string> listB = new List<string>();
        lock (this) {
            using (var fs = File.OpenRead(@"C:\Users\stag01\Desktop\identifiants.csv"))
            using (var reader = new StreamReader(fs)) {

                while (!reader.EndOfStream) {
                    var line = reader.ReadLine();
                    var values = line.Split(';');

                    listA.Add(values[0]);
                    listB.Add(values[1]);
                }
            } 
        } 
        return listA;

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 2018-01-20
    • 1970-01-01
    • 2012-12-21
    • 2018-05-24
    • 2013-05-03
    • 1970-01-01
    相关资源
    最近更新 更多