Search code examples
c#infopath-forms-services

C# infopath directory.getfiles access error


I need to use an infopath form and its embedded code to search a directory for files with a specific extension. This code will be used to populate a multi-line textbox based on the findings of the scan. As of now, I have it spitting out the error message to the form inside the catch statement.

        string directory = @"C:\Users\am21820\Desktop";

        try
        {
            string[] files = Directory.GetFiles(directory, "*.prm", SearchOption.TopDirectoryOnly);
            string result1 = ConvertStringArrayToString(files);
            MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Test_Field", this.NamespaceManager).SetValue(result1);
        }

        catch (Exception f)
        {
            MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Test_Field", this.NamespaceManager).SetValue(f.Message);
        }

I have tried the following in addition to what is above.

        //FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.AllAccess, directory);
        //permission.AllLocalFiles = FileIOPermissionAccess.AllAccess;

        //try
        //{
        //    permission.Assert();
        //    permission.Demand();
        //}

        //catch (Exception f)
        //{
        //    MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:Test_Field", this.NamespaceManager).SetValue(f.Message);
        //}

I am using InfoPath 2013 along with VS 2012 and VSTA was updated along with my transition to InfoPath 2013. I get the following error. This error is present no matter which directory I try to scan.

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.


Solution

  • By design. You need full trust form to be able to access files.

    Note that for Forms Services C# code runs on server - and you can't get any access to user local files.