I am making a form that will be used by people on the exact same computer and then it will be saved locally. It is to store data for delivery guys, so they fill in some information on a form and then head out. Now at night, all these forms will be processed by another script (using Robotic Process Automation).
So I needed to make a button that would save the form locally with a certain unique filename and would then close the form. This all works fine on my local machine, with a "full trust" security level and using the following code:
public void CTRL1_5_Clicked(object sender, ClickedEventArgs e)
{
string projectTitle = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:ID", this.NamespaceManager).Value; if (projectTitle == null || projectTitle == "")
{
projectTitle = "IncorrectIdentifier";
}
string currentDirect = Directory.GetCurrentDirectory();
this.SaveAs(currentDirect + "/" + projectTitle + ".XML");
//the line below also closes the application. If one need to just close the form, use this.Close() instead.
//System.Environment.Exit(0);
}
Now the problem arises when I move this form into my work environment. By the system settings it will not allow a form with "Full trust". So I thought to change the trust level to "domain", but then it seems that all the "System.IO" calls (such as "Directory.GetCurrentDirectory()" or more importantly "this.SaveAs" and "System.Environment.Exit(0)" ) will not work.
I do not think I can reach my system administrator to allow for full trust forms. So I really wonder whether it is possible to save a file within a domain trust setting. The file will be stored locally, in the same map as the form is openend, so the user account from which the form is accessed will have rights to read and write within the folder. But I still get the following error:
Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
at System.Security.CodeAccessPermission.Demand()
at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
at System.IO.Directory.InternalGetCurrentDirectory(Boolean checkHost)
at System.IO.Directory.GetCurrentDirectory()
at coded.FormCode.CTRL1_5_Clicked(Object sender, ClickedEventArgs e)
at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)
This is pointed to the "GetCurrentDirectory" command, but if I change that it will point to the "SaveAs" command and if I change that it will point to the "Environment.Exit" command. And I basically need all three.
Any help would be much appreciated.