diff --git a/SampleApplications/SDK/Opc.Ua.Configuration/AccountInfo.cs b/SampleApplications/SDK/Opc.Ua.Configuration/AccountInfo.cs index e52bf954e2..aec2dd9a25 100644 --- a/SampleApplications/SDK/Opc.Ua.Configuration/AccountInfo.cs +++ b/SampleApplications/SDK/Opc.Ua.Configuration/AccountInfo.cs @@ -108,7 +108,7 @@ namespace Opc.Ua.Configuration if (!String.IsNullOrEmpty(m_domain)) { - return Utils.Format(@"{0}\{1}", m_domain, m_name); + return Utils.Format("{0}{1}{2}", m_domain, Path.DirectorySeparatorChar, m_name); } return m_name; diff --git a/SampleApplications/SDK/Opc.Ua.Configuration/ApplicationInstance.cs b/SampleApplications/SDK/Opc.Ua.Configuration/ApplicationInstance.cs index 2e048e95e1..48050f42d0 100644 --- a/SampleApplications/SDK/Opc.Ua.Configuration/ApplicationInstance.cs +++ b/SampleApplications/SDK/Opc.Ua.Configuration/ApplicationInstance.cs @@ -36,6 +36,7 @@ using System.Runtime.Serialization; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using System.Net; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua.Configuration { @@ -243,7 +244,7 @@ namespace Opc.Ua.Configuration // update fixed fields in the installation config. InstallConfig.ApplicationType = (Opc.Ua.Security.ApplicationType)(int)ApplicationType; - InstallConfig.ExecutableFile = ApplicationData.Current.LocalFolder.Path; + InstallConfig.ExecutableFile = PlatformServices.Default.Application.ApplicationBasePath; if (InstallConfig.TraceConfiguration != null) { @@ -747,7 +748,11 @@ namespace Opc.Ua.Configuration try { // install the GDS agent configuration file - string agentPath = Utils.GetAbsoluteDirectoryPath(ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\GDS\\Applications", false, false, true); + string agentPath = Utils.GetAbsoluteDirectoryPath( + PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "GDS" + Path.DirectorySeparatorChar + "Applications", + false, + false, + true); if (agentPath != null) { @@ -756,7 +761,7 @@ namespace Opc.Ua.Configuration DataContractSerializer serializer = new DataContractSerializer(typeof(Opc.Ua.Security.SecuredApplication)); - using (FileStream ostrm = File.Open(agentPath + "\\" + configuration.ApplicationName + ".xml", FileMode.Create)) + using (FileStream ostrm = File.Open(agentPath + Path.DirectorySeparatorChar + configuration.ApplicationName + ".xml", FileMode.Create)) { serializer.WriteObject(ostrm, export); Utils.Trace(Utils.TraceMasks.Information, "Created GDS agent configuration file."); @@ -803,11 +808,15 @@ namespace Opc.Ua.Configuration { try { - string agentPath = Utils.GetAbsoluteDirectoryPath(ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\GDS\\Applications", false, false, false); + string agentPath = Utils.GetAbsoluteDirectoryPath( + PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "GDS" + Path.DirectorySeparatorChar + "Applications", + false, + false, + false); if (agentPath != null) { - File.Delete(agentPath + "\\" + configuration.ApplicationName + ".xml"); + File.Delete(agentPath + Path.DirectorySeparatorChar + configuration.ApplicationName + ".xml"); } } catch (Exception e) @@ -1341,7 +1350,7 @@ namespace Opc.Ua.Configuration try { - string configurationPath = Utils.GetAbsoluteFilePath(ApplicationData.Current.LocalFolder.Path + @"\OPC Foundation\Config\Opc.Ua.DiscoveryServer.Config.xml", true, false, false); + string configurationPath = Utils.GetAbsoluteFilePath(PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "Config" + Path.DirectorySeparatorChar + "Opc.Ua.DiscoveryServer.Config.xml", true, false, false); if (configurationPath == null) { diff --git a/SampleApplications/SDK/Opc.Ua.Configuration/ConfigUtils.cs b/SampleApplications/SDK/Opc.Ua.Configuration/ConfigUtils.cs index b20f13c4f8..0335749407 100644 --- a/SampleApplications/SDK/Opc.Ua.Configuration/ConfigUtils.cs +++ b/SampleApplications/SDK/Opc.Ua.Configuration/ConfigUtils.cs @@ -36,6 +36,7 @@ using System.Security.Cryptography.X509Certificates; using System.IO; using System.Xml; using System.Threading.Tasks; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua.Configuration { @@ -58,8 +59,8 @@ namespace Opc.Ua.Configuration public static string GetLogFileDirectory() { // try the program data directory. - string logFileDirectory = ApplicationData.Current.LocalFolder.Path; - logFileDirectory += "\\OPC Foundation\\Logs"; + string logFileDirectory = PlatformServices.Default.Application.ApplicationBasePath; + logFileDirectory += Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "Logs"; try { @@ -101,8 +102,8 @@ namespace Opc.Ua.Configuration catch (Exception) { // try the MyDocuments directory instead. - logFileDirectory = ApplicationData.Current.LocalFolder.Path; - logFileDirectory += "OPC Foundation\\Logs"; + logFileDirectory = PlatformServices.Default.Application.ApplicationBasePath; + logFileDirectory += "OPC Foundation" + Path.DirectorySeparatorChar + "Logs"; if (!Directory.Exists(logFileDirectory)) { @@ -230,7 +231,7 @@ namespace Opc.Ua.Configuration { application.ApplicationCertificate = new Opc.Ua.Security.CertificateIdentifier(); application.ApplicationCertificate.StoreType = Utils.DefaultStoreType; - application.ApplicationCertificate.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.ApplicationCertificate.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "MachineDefault"; application.ApplicationCertificate.SubjectName = application.ApplicationName; } @@ -238,14 +239,14 @@ namespace Opc.Ua.Configuration { application.IssuerCertificateStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.IssuerCertificateStore.StoreType = Utils.DefaultStoreType; - application.IssuerCertificateStore.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.IssuerCertificateStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "MachineDefault"; } if (application.TrustedCertificateStore == null) { application.TrustedCertificateStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.TrustedCertificateStore.StoreType = Utils.DefaultStoreType; - application.TrustedCertificateStore.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.TrustedCertificateStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar + "MachineDefault"; } try @@ -261,7 +262,7 @@ namespace Opc.Ua.Configuration { application.RejectedCertificatesStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.RejectedCertificatesStore.StoreType = CertificateStoreType.Directory; - application.RejectedCertificatesStore.StorePath = ApplicationData.Current.LocalFolder.Path + Path.DirectorySeparatorChar + "Rejected"; + application.RejectedCertificatesStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "Rejected"; } if (application.RejectedCertificatesStore.StoreType == CertificateStoreType.Directory) @@ -297,8 +298,9 @@ namespace Opc.Ua.Configuration if (String.IsNullOrEmpty(application.ConfigurationFile)) { application.ConfigurationFile = Utils.Format( - "{0}\\{1}.Config.xml", - executableFileInfo.DirectoryName, + "{0}{1}{2}.Config.xml", + executableFileInfo.DirectoryName, + Path.DirectorySeparatorChar, applicationName); } @@ -349,9 +351,9 @@ namespace Opc.Ua.Configuration try { string installationFile = Utils.Format( - "{0}\\Install\\{1}.Config.xml", - executableFileInfo.Directory.Parent.FullName, - applicationName); + "{0}Install{1}.Config.xml", + executableFileInfo.Directory.Parent.FullName + Path.DirectorySeparatorChar, + Path.DirectorySeparatorChar + applicationName); if (!File.Exists(installationFile)) { @@ -393,7 +395,7 @@ namespace Opc.Ua.Configuration { application.ApplicationCertificate = new Opc.Ua.Security.CertificateIdentifier(); application.ApplicationCertificate.StoreType = Utils.DefaultStoreType; - application.ApplicationCertificate.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.ApplicationCertificate.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "MachineDefault"; } if (application.UseDefaultCertificateStores) @@ -402,14 +404,14 @@ namespace Opc.Ua.Configuration { application.IssuerCertificateStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.IssuerCertificateStore.StoreType = Utils.DefaultStoreType; - application.IssuerCertificateStore.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.IssuerCertificateStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "MachineDefault"; } if (application.TrustedCertificateStore == null) { application.TrustedCertificateStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.TrustedCertificateStore.StoreType = Utils.DefaultStoreType; - application.TrustedCertificateStore.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\MachineDefault"; + application.TrustedCertificateStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "MachineDefault"; } try @@ -425,13 +427,13 @@ namespace Opc.Ua.Configuration { application.RejectedCertificatesStore = new Opc.Ua.Security.CertificateStoreIdentifier(); application.RejectedCertificatesStore.StoreType = CertificateStoreType.Directory; - application.RejectedCertificatesStore.StorePath = ApplicationData.Current.LocalFolder.Path + "\\OPC Foundation\\CertificateStores\\RejectedCertificates"; + application.RejectedCertificatesStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "OPC Foundation" + Path.DirectorySeparatorChar + "CertificateStores" + Path.DirectorySeparatorChar + "RejectedCertificates"; StringBuilder buffer = new StringBuilder(); - buffer.Append(ApplicationData.Current.LocalFolder.Path); - buffer.Append("\\OPC Foundation"); - buffer.Append("\\RejectedCertificates"); + buffer.Append(PlatformServices.Default.Application.ApplicationBasePath); + buffer.Append(Path.DirectorySeparatorChar + "OPC Foundation"); + buffer.Append(Path.DirectorySeparatorChar + "RejectedCertificates"); string folderPath = buffer.ToString(); @@ -700,8 +702,9 @@ namespace Opc.Ua.Configuration if (String.IsNullOrEmpty(application.ConfigurationFile)) { application.ConfigurationFile = Utils.Format( - "{0}\\{1}.Config.xml", - executableFileInfo.DirectoryName, + "{0}{1}{2}.Config.xml", + executableFileInfo.DirectoryName, + Path.DirectorySeparatorChar, applicationName); } diff --git a/SampleApplications/SDK/Opc.Ua.Configuration/ManagedApplication.cs b/SampleApplications/SDK/Opc.Ua.Configuration/ManagedApplication.cs index 9fe4103dec..7eb936864d 100644 --- a/SampleApplications/SDK/Opc.Ua.Configuration/ManagedApplication.cs +++ b/SampleApplications/SDK/Opc.Ua.Configuration/ManagedApplication.cs @@ -34,6 +34,7 @@ using System.IO; using System.Runtime.Serialization; using System.Xml; using System.Threading.Tasks; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua.Configuration { @@ -202,35 +203,6 @@ namespace Opc.Ua.Configuration } } - /// - /// Saves the specified file path. - /// - /// The file path. Uses the original source file path if not provided. - public void Save(string filePath) - { - if (String.IsNullOrEmpty(filePath)) - { - if (m_sourceFile == null) - { - filePath = Utils.GetAbsoluteDirectoryPath("%LocalApplicationData%\\OPC Foundation\\Applications\\", false, false, true); - filePath += m_displayName; - filePath += "*.xml"; - } - else - { - filePath = m_sourceFile.FullName; - } - } - - using (Stream ostrm = File.Open(filePath, FileMode.Create)) - { - DataContractSerializer serializer = new DataContractSerializer(typeof(ManagedApplication)); - serializer.WriteObject(ostrm, this); - } - - m_sourceFile = new FileInfo(filePath); - } - /// /// Sets the executable file. /// @@ -304,7 +276,7 @@ namespace Opc.Ua.Configuration } FileInfo executablePath = new FileInfo(m_executablePath); - string currentDirectory = ApplicationData.Current.LocalFolder.Path; + string currentDirectory = PlatformServices.Default.Application.ApplicationBasePath; try { diff --git a/SampleApplications/SDK/Opc.Ua.Configuration/Schema/InstalledApplicationHelper.cs b/SampleApplications/SDK/Opc.Ua.Configuration/Schema/InstalledApplicationHelper.cs index 7ea0e41dcc..7329a5d3ad 100644 --- a/SampleApplications/SDK/Opc.Ua.Configuration/Schema/InstalledApplicationHelper.cs +++ b/SampleApplications/SDK/Opc.Ua.Configuration/Schema/InstalledApplicationHelper.cs @@ -29,6 +29,7 @@ using System.Runtime.Serialization; using System.IO; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua.Configuration { @@ -48,7 +49,7 @@ namespace Opc.Ua.Configuration // look in current directory. if (!file.Exists) { - file = new FileInfo(Utils.Format("{0}\\{1}", ApplicationData.Current.LocalFolder.Path, filePath)); + file = new FileInfo(Utils.Format("{0}{1}{2}", PlatformServices.Default.Application.ApplicationBasePath, Path.DirectorySeparatorChar, filePath)); } // look in executable directory. @@ -64,7 +65,7 @@ namespace Opc.Ua.Configuration StatusCodes.BadConfigurationError, "File does not exist: {0}\r\nCurrent directory is: {1}", filePath, - ApplicationData.Current.LocalFolder.Path); + PlatformServices.Default.Application.ApplicationBasePath); } return Load(file); diff --git a/SampleApplications/SDK/Opc.Ua.Server/AddressSpace/DataTypeDictionary.cs b/SampleApplications/SDK/Opc.Ua.Server/AddressSpace/DataTypeDictionary.cs deleted file mode 100644 index 56d3dce7d2..0000000000 --- a/SampleApplications/SDK/Opc.Ua.Server/AddressSpace/DataTypeDictionary.cs +++ /dev/null @@ -1,314 +0,0 @@ -/* ======================================================================== - * Copyright (c) 2005-2016 The OPC Foundation, Inc. All rights reserved. - * - * OPC Foundation MIT License 1.00 - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * The complete license agreement can be found here: - * http://opcfoundation.org/License/MIT/1.00/ - * ======================================================================*/ - -using System; -using System.Collections.Generic; -using System.Text; -using System.Xml; -using System.Runtime.Serialization; -using System.Reflection; -using System.IO; - -namespace Opc.Ua.Server -{ -#if LEGACY_CORENODEMANAGER - /// - /// A variable that contains live automation data. - /// - [Obsolete("The DataTypeDictionary class is obsolete and is not supported. See Opc.Ua.DataTypeDictionaryState for a replacement.")] - public partial class DataTypeDictionary - { - #region Public Methods - /// - /// Sets an embedded resource. - /// - public void SetDictionarySource(Assembly assembly, string resourcePath, string namespaceUri) - { - SpecifyDataTypeVersion(Property.Construct(Server)); - SpecifyNamespaceUri(Property.Construct(Server)); - - lock (DataLock) - { - m_assembly = assembly; - m_filePath = resourcePath; - MinimumSamplingInterval = MinimumSamplingIntervals.Continuous; - UpdateValue(null, StatusCodes.BadWaitingForInitialData); - - DataTypeVersion.Value = Utils.Format("{0}", m_assembly.GetName().Version); - NamespaceUri.Value = namespaceUri; - } - } - - /// - /// Sets a file as the dictionary source and specifies a minimum interval between scans. - /// - public void SetDictionarySource(string fileName, string searchPath, int minimumScanRate) - { - lock (DataLock) - { - // construct the filepath. - FileInfo fileInfo = new FileInfo(fileName); - - if (!fileInfo.Exists) - { - if (String.IsNullOrEmpty(searchPath)) - { - searchPath = GetDefaultHttpPath(); - } - - if (!String.IsNullOrEmpty(searchPath)) - { - if (searchPath.StartsWith(Uri.UriSchemeHttp)) - { - if (!searchPath.EndsWith("/")) - { - searchPath += "/"; - } - - fileName = String.Format("{0}{1}", searchPath, fileName); - } - else - { - if (!searchPath.EndsWith("\\")) - { - searchPath += "\\"; - } - - fileName = String.Format("{0}{1}", searchPath, fileName); - } - } - } - - m_filePath = fileName; - MinimumSamplingInterval = minimumScanRate; - UpdateValue(null, StatusCodes.BadWaitingForInitialData); - } - } - #endregion - - #region I/O Support Functions - /// - /// Returns the data type path for the DataTypeDescription. - /// - protected virtual string GetDataTypePath(string typeName) - { - string path = typeName; - - if (m_typeSystemId == Objects.XmlSchema_TypeSystem) - { - path = String.Format("//xs:element[@name='{0}']", typeName); - } - - return path; - } - - /// - /// Returns the data dictionary from cache or disk (updates the data type version as well). - /// - protected virtual byte[] ReadDictionary(double maxAge) - { - byte[] dictionary = Value; - - // return cached value. - if (dictionary != null) - { - if (Timestamp.AddMilliseconds(MinimumSamplingInterval) > DateTime.UtcNow) - { - return dictionary; - } - - if (Timestamp.AddMilliseconds(maxAge) > DateTime.UtcNow) - { - return dictionary; - } - - if (m_assembly != null) - { - return dictionary; - } - } - - // read from source. - try - { - if (m_assembly != null) - { - dictionary = ReadDictionaryFromResource(m_filePath); - } - else if (m_filePath.StartsWith(Uri.UriSchemeHttp)) - { - dictionary = ReadDictionaryFromHttpFile(m_filePath); - } - else - { - dictionary = ReadDictionaryFromDiskFile(m_filePath); - } - - return dictionary; - } - catch (Exception e) - { - ServiceResult error = ServiceResult.Create(e, StatusCodes.BadOutOfService, "Could not access data dictionary file: {0}.", m_filePath); - UpdateStatus(error); - return null; - } - } - - /// - /// Returns the default HTTP path to use from the hosting process. - /// - protected virtual string GetDefaultHttpPath() - { - foreach (Uri endpointAddress in Server.EndpointAddresses) - { - if (!endpointAddress.Scheme.StartsWith(Uri.UriSchemeHttp)) - { - continue; - } - - string url = endpointAddress.ToString(); - - int index = url.LastIndexOf('/'); - - if (index != -1) - { - url = url.Substring(0, index+1); - } - - return url; - } - - return null; - } - - /// - /// Loads a file from a HTTP URL. - /// - protected virtual byte[] ReadDictionaryFromHttpFile(string url) - { - System.Net.WebRequest request = System.Net.WebRequest.Create(url); - System.Net.WebResponse response = request.GetResponse(); - Stream istrm = response.GetResponseStream(); - - byte[] dictionary = ReadDictionary(istrm); - - // use the current time as the dictionary version. - m_dataTypeVersion.Value = XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc); - - return dictionary; - } - - /// - /// Reads the dictionary from a disk file. - /// - protected virtual byte[] ReadDictionaryFromDiskFile(string filePath) - { - Stream istrm = File.Open(filePath, FileMode.Open); - - byte[] dictionary = ReadDictionary(istrm); - - // use the file modification time as the dictionary version. - m_dataTypeVersion.Value = XmlConvert.ToString(new FileInfo(filePath).LastWriteTimeUtc, XmlDateTimeSerializationMode.Utc); - - return dictionary; - } - - /// - /// Reads the dictionary from an embedded resource. - /// - protected virtual byte[] ReadDictionaryFromResource(string filePath) - { - Stream istrm = m_assembly.GetManifestResourceStream(filePath); - - byte[] dictionary = ReadDictionary(istrm); - - // use the assembly version as the version. - DataTypeVersion.Value = Utils.Format("{0}", m_assembly.GetName().Version); - - return dictionary; - } - - /// - /// Reads the dictionary from the stream. - /// - protected virtual byte[] ReadDictionary(Stream istrm) - { - BinaryReader reader = new BinaryReader(istrm); - MemoryStream ostrm = new MemoryStream(); - - try - { - byte[] buffer = new byte[4096]; - - while (true) - { - int bytesRead = reader.Read(buffer, 0, buffer.Length); - - if (bytesRead <= 0) - { - break; - } - - ostrm.Write(buffer, 0, bytesRead); - } - - ostrm.Close(); - return ostrm.ToArray(); - } - finally - { - reader.Close(); - } - } - #endregion - - #region Overridden Methods - /// - /// Looks up the type system id. - /// - protected override void OnAfterCreate(object configuration) - { - base.OnAfterCreate(configuration); - - m_typeSystemId = NodeManager.FindTargetId( - this.NodeId, - ReferenceTypeIds.Organizes, - true, - null); - } - #endregion - - #region Private Fields - private NodeId m_typeSystemId; - private Assembly m_assembly; - private string m_filePath; - #endregion - } -#endif -} diff --git a/SampleApplications/Samples/NetCoreConsoleClient/Program.cs b/SampleApplications/Samples/NetCoreConsoleClient/Program.cs index b1c56436cc..7ceb1170f4 100644 --- a/SampleApplications/Samples/NetCoreConsoleClient/Program.cs +++ b/SampleApplications/Samples/NetCoreConsoleClient/Program.cs @@ -44,19 +44,19 @@ namespace NetCoreConsoleClient { ApplicationCertificate = new CertificateIdentifier { - StoreType = @"Directory", - StorePath = @"", - SubjectName = Utils.Format(@"CN={0}, DC={1}", "UA Sample Client", Utils.GetHostName()) + StoreType = "Directory", + StorePath = "", + SubjectName = Utils.Format("CN={0}, DC={1}", "UA Sample Client", Utils.GetHostName()) }, TrustedPeerCertificates = new CertificateTrustList { - StoreType = @"Directory", - StorePath = @"", + StoreType = "Directory", + StorePath = "", }, TrustedIssuerCertificates = new CertificateTrustList { - StoreType = @"Directory", - StorePath = @"", + StoreType = "Directory", + StorePath = "", }, NonceLength = 32, AutoAcceptUntrustedCertificates = true diff --git a/Stack/Opc.Ua.Core/ApplicationData.cs b/Stack/Opc.Ua.Core/ApplicationData.cs deleted file mode 100644 index 4612ee548b..0000000000 --- a/Stack/Opc.Ua.Core/ApplicationData.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.Extensions.PlatformAbstractions; -using System.Collections; - -namespace Opc.Ua -{ - - public static class ApplicationData - { - - public static class Current - { - public static class LocalFolder - { - public static string Path = PlatformServices.Default.Application.ApplicationBasePath; - } - public static class LocalSettings - { - public static ApplicationDataContainer CreateContainer(string application, ApplicationDataCreateDisposition adcd) - { - return new ApplicationDataContainer(); - } - } - } - } - public enum ApplicationDataCreateDisposition - { - Always = 0, - Existing = 1 - } - - public class ApplicationDataContainer - { - public IDictionary Values; - } -} diff --git a/Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs b/Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs index 7cb07ebeff..33733ecaeb 100644 --- a/Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs +++ b/Stack/Opc.Ua.Core/Schema/ApplicationConfiguration.cs @@ -10,6 +10,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ +using Microsoft.Extensions.PlatformAbstractions; using System; using System.Collections.Generic; using System.IO; @@ -1971,13 +1972,13 @@ namespace Opc.Ua set { - if (value.StartsWith(ApplicationData.Current.LocalFolder.Path) == true) + if (value.StartsWith(PlatformServices.Default.Application.ApplicationBasePath) == true) { m_storePath = value; } else { - m_storePath = ApplicationData.Current.LocalFolder.Path; + m_storePath = PlatformServices.Default.Application.ApplicationBasePath; if (!String.IsNullOrEmpty(value)) { m_storePath += Path.DirectorySeparatorChar + value; @@ -2238,13 +2239,13 @@ namespace Opc.Ua set { - if (value.StartsWith(ApplicationData.Current.LocalFolder.Path)) + if (value.StartsWith(PlatformServices.Default.Application.ApplicationBasePath)) { m_storePath = value; } else { - m_storePath = ApplicationData.Current.LocalFolder.Path; + m_storePath = PlatformServices.Default.Application.ApplicationBasePath; if (!String.IsNullOrEmpty(value)) { m_storePath += Path.DirectorySeparatorChar + value; diff --git a/Stack/Opc.Ua.Core/Security/AccessControl/AccessTemplateManager.cs b/Stack/Opc.Ua.Core/Security/AccessControl/AccessTemplateManager.cs index 1e81edf0c1..3cea868ce6 100644 --- a/Stack/Opc.Ua.Core/Security/AccessControl/AccessTemplateManager.cs +++ b/Stack/Opc.Ua.Core/Security/AccessControl/AccessTemplateManager.cs @@ -62,7 +62,7 @@ namespace Opc.Ua /// public static void DeleteTemplate(string directory, string templateName) { - string filePath = directory + " \\" + templateName + m_FileExtension; + string filePath = directory + Path.DirectorySeparatorChar + templateName + m_FileExtension; AccessTemplateManager.DeleteFile(filePath); } diff --git a/Stack/Opc.Ua.Core/Security/AccessControl/UserRoleManager.cs b/Stack/Opc.Ua.Core/Security/AccessControl/UserRoleManager.cs index 4e3e9f622f..59a6101c75 100644 --- a/Stack/Opc.Ua.Core/Security/AccessControl/UserRoleManager.cs +++ b/Stack/Opc.Ua.Core/Security/AccessControl/UserRoleManager.cs @@ -93,7 +93,7 @@ namespace Opc.Ua /// public static void DeleteRole(string directory, string roleName) { - string filePath = directory + " \\" + roleName + m_FileExtension; + string filePath = directory + Path.DirectorySeparatorChar + roleName + m_FileExtension; AccessTemplateManager.DeleteFile(filePath); } diff --git a/Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs b/Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs index 44aced1e23..96e700d22d 100644 --- a/Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs +++ b/Stack/Opc.Ua.Core/Security/Certificates/CertificateFactory.cs @@ -17,6 +17,7 @@ using System.Security.Cryptography.X509Certificates; using System.Text; using System.IO; using System.Threading.Tasks; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua { @@ -332,7 +333,7 @@ namespace Opc.Ua { ICertificateStore store = new Opc.Ua.DirectoryCertificateStore(); - await store.Open(storePath); + store.Open(storePath); await store.Add(certificate); } @@ -347,7 +348,7 @@ namespace Opc.Ua string executablePath = null; //first check on the same folder as the current executable - executablePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Opc.Ua.CertificateGenerator.exe"); + executablePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Opc.Ua.CertificateGenerator.exe"); executablePath = Utils.GetAbsoluteFilePath(executablePath, false, false, false); if (executablePath != null) @@ -358,12 +359,12 @@ namespace Opc.Ua // recursively go up the tree looking for /Bin directories. if (executablePath == null) { - DirectoryInfo dirInfo = new DirectoryInfo(ApplicationData.Current.LocalFolder.Path); + DirectoryInfo dirInfo = new DirectoryInfo(PlatformServices.Default.Application.ApplicationBasePath); while (dirInfo != null) { executablePath = dirInfo.FullName; - executablePath += "\\Bin\\Opc.Ua.CertificateGenerator.exe"; + executablePath += (Path.DirectorySeparatorChar + "Bin" + Path.DirectorySeparatorChar + "Opc.Ua.CertificateGenerator.exe"); executablePath = Utils.GetAbsoluteFilePath(executablePath, false, false, false); if (executablePath != null) @@ -375,15 +376,15 @@ namespace Opc.Ua } } - // hard the the proxy path name for now. - string commonFilesDir = Utils.GetAbsoluteDirectoryPath(@"%CommonProgramFiles%", false, false); - string relativePath = @"\OPC Foundation\UA\v1.0\Bin\Opc.Ua.CertificateGenerator.exe"; + // hardcode the proxy path name for now. + string commonFilesDir = Utils.GetAbsoluteDirectoryPath("%CommonProgramFiles%", false, false); + string relativePath = Path.DirectorySeparatorChar + "OPC Foundation"+ Path.DirectorySeparatorChar + "UA" + Path.DirectorySeparatorChar + "v1.0" + Path.DirectorySeparatorChar + "Bin" + Path.DirectorySeparatorChar + "Opc.Ua.CertificateGenerator.exe"; executablePath = Utils.GetAbsoluteFilePath(commonFilesDir + relativePath, false, false, false); if (executablePath == null) { // try the x86 directory. - int index = commonFilesDir.LastIndexOf('\\', commonFilesDir.Length - 2); + int index = commonFilesDir.LastIndexOf(Path.DirectorySeparatorChar, commonFilesDir.Length - 2); if (index != -1) { @@ -588,7 +589,7 @@ namespace Opc.Ua // load the new certificate from the store. ICertificateStore store = new Opc.Ua.DirectoryCertificateStore(); - await store.Open(storePath); + store.Open(storePath); X509Certificate2Collection certificates = await store.FindByThumbprint(thumbprint); store.Close(); @@ -743,7 +744,7 @@ namespace Opc.Ua { X509Certificate2 certificate = await CreateCertificate( CertificateStoreType.Windows, - storeLocation + "\\" + storeName, + storeLocation + Path.DirectorySeparatorChar + storeName, applicationUri, applicationName, null, diff --git a/Stack/Opc.Ua.Core/Security/Certificates/CertificateIdentifier.cs b/Stack/Opc.Ua.Core/Security/Certificates/CertificateIdentifier.cs index 491dc1d452..9c6eb7ba79 100644 --- a/Stack/Opc.Ua.Core/Security/Certificates/CertificateIdentifier.cs +++ b/Stack/Opc.Ua.Core/Security/Certificates/CertificateIdentifier.cs @@ -168,7 +168,7 @@ namespace Opc.Ua { using (DirectoryCertificateStore store = new DirectoryCertificateStore()) { - await store.Open(this.StorePath); + store.Open(this.StorePath); m_certificate = store.LoadPrivateKey(this.Thumbprint, this.SubjectName, password); return m_certificate; } @@ -198,7 +198,7 @@ namespace Opc.Ua // open store. using (ICertificateStore store = CertificateStoreIdentifier.CreateStore(StoreType)) { - await store.Open(StorePath); + store.Open(StorePath); X509Certificate2Collection collection = await store.Enumerate(); @@ -490,7 +490,7 @@ namespace Opc.Ua try { - await store.Open(this.StorePath); + store.Open(this.StorePath); return store.GetPrivateKeyFilePath(certificate.Thumbprint); } finally diff --git a/Stack/Opc.Ua.Core/Security/Certificates/DirectoryCertificateStore.cs b/Stack/Opc.Ua.Core/Security/Certificates/DirectoryCertificateStore.cs index 95f403cbf8..03a99095fe 100644 --- a/Stack/Opc.Ua.Core/Security/Certificates/DirectoryCertificateStore.cs +++ b/Stack/Opc.Ua.Core/Security/Certificates/DirectoryCertificateStore.cs @@ -77,46 +77,14 @@ namespace Opc.Ua #region ICertificateStore Members /// - public async Task Open(string location) + public void Open(string location) { -#if TODO - bool certsInRemovableStorageRootFound = false; - IReadOnlyList folders = new List(); - - try - { - folders = await KnownFolders.RemovableDevices.GetFoldersAsync(); - if (folders.Count > 0) - { - IReadOnlyList files = await folders[0].GetFilesAsync(); - if (files.Count > 0) - { - certsInRemovableStorageRootFound = true; - } - } - } - catch (Exception) - { - // do nothing - } -#endif lock (m_lock) { -#if TODO - if (certsInRemovableStorageRootFound && (folders.Count > 0)) - { - m_directory = new DirectoryInfo(folders[0].Path); - m_certificateSubdir = m_directory; - m_privateKeySubdir = m_directory; - } - else -#endif - { - location = Utils.ReplaceSpecialFolderNames(location); - m_directory = new DirectoryInfo(location); - m_certificateSubdir = new DirectoryInfo(m_directory.FullName + Path.DirectorySeparatorChar + "certs"); - m_privateKeySubdir = new DirectoryInfo(m_directory.FullName + Path.DirectorySeparatorChar + "private"); - } + location = Utils.ReplaceSpecialFolderNames(location); + m_directory = new DirectoryInfo(location); + m_certificateSubdir = new DirectoryInfo(m_directory.FullName + Path.DirectorySeparatorChar + "certs"); + m_privateKeySubdir = new DirectoryInfo(m_directory.FullName + Path.DirectorySeparatorChar + "private"); } } diff --git a/Stack/Opc.Ua.Core/Security/Certificates/ICertificateStore.cs b/Stack/Opc.Ua.Core/Security/Certificates/ICertificateStore.cs index da32bde7d4..4c04b2bed6 100644 --- a/Stack/Opc.Ua.Core/Security/Certificates/ICertificateStore.cs +++ b/Stack/Opc.Ua.Core/Security/Certificates/ICertificateStore.cs @@ -29,7 +29,7 @@ namespace Opc.Ua /// /// The syntax depends on the store implementation. /// - Task Open(string location); + void Open(string location); /// /// Closes the store. diff --git a/Stack/Opc.Ua.Core/Security/Certificates/SecurityConfiguration.cs b/Stack/Opc.Ua.Core/Security/Certificates/SecurityConfiguration.cs index b17559bc11..5aa4be8a0f 100644 --- a/Stack/Opc.Ua.Core/Security/Certificates/SecurityConfiguration.cs +++ b/Stack/Opc.Ua.Core/Security/Certificates/SecurityConfiguration.cs @@ -12,6 +12,7 @@ +using Microsoft.Extensions.PlatformAbstractions; using System.IO; namespace Opc.Ua @@ -49,7 +50,7 @@ namespace Opc.Ua { RejectedCertificateStore = new CertificateStoreIdentifier(); RejectedCertificateStore.StoreType = CertificateStoreType.Directory; - RejectedCertificateStore.StorePath = ApplicationData.Current.LocalFolder.Path + Path.DirectorySeparatorChar + "Rejected"; + RejectedCertificateStore.StorePath = PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + "Rejected"; } } diff --git a/Stack/Opc.Ua.Core/Stack/Configuration/ApplicationConfiguration.cs b/Stack/Opc.Ua.Core/Stack/Configuration/ApplicationConfiguration.cs index d864edf688..e5340ee36d 100644 --- a/Stack/Opc.Ua.Core/Stack/Configuration/ApplicationConfiguration.cs +++ b/Stack/Opc.Ua.Core/Stack/Configuration/ApplicationConfiguration.cs @@ -10,6 +10,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ +using Microsoft.Extensions.PlatformAbstractions; using System; using System.Collections.Generic; using System.IO; @@ -241,7 +242,7 @@ namespace Opc.Ua StatusCodes.BadConfigurationError, "Configuration file does not exist: {0}\r\nCurrent directory is: {1}", filePath, - ApplicationData.Current.LocalFolder.Path); + PlatformServices.Default.Application.ApplicationBasePath); } return await Load(file, applicationType, systemType); @@ -506,8 +507,8 @@ namespace Opc.Ua if (filePath == null) { filePath = m_clientConfiguration.EndpointCacheFilePath; - // TODO: //// - if (!filePath.StartsWith("\\\\", StringComparison.Ordinal) && filePath.IndexOf(":", StringComparison.Ordinal) != 1) + + if (!Path.IsPathRooted(filePath)) { FileInfo sourceFile = new FileInfo(this.SourceFilePath); filePath = Utils.Format("{0}{1}{2}", sourceFile.DirectoryName, Path.DirectorySeparatorChar, filePath); diff --git a/Stack/Opc.Ua.Core/Types/Schemas/SchemaValidator.cs b/Stack/Opc.Ua.Core/Types/Schemas/SchemaValidator.cs index cd5851cb8f..8d0f9c72e3 100644 --- a/Stack/Opc.Ua.Core/Types/Schemas/SchemaValidator.cs +++ b/Stack/Opc.Ua.Core/Types/Schemas/SchemaValidator.cs @@ -17,6 +17,7 @@ using System.Xml.Serialization; using System.IO; using System.Reflection; using System.Globalization; +using Microsoft.Extensions.PlatformAbstractions; namespace Opc.Ua.Schema { @@ -201,7 +202,7 @@ namespace Opc.Ua.Schema // check for file in the same directory as the input file. FileInfo inputInfo = new FileInfo(m_inputPath); - fileInfo = new FileInfo(inputInfo.DirectoryName + "\\" + fileInfo.Name); + fileInfo = new FileInfo(inputInfo.DirectoryName + Path.DirectorySeparatorChar + fileInfo.Name); if (fileInfo.Exists) { @@ -209,7 +210,7 @@ namespace Opc.Ua.Schema } // check for file in the process directory. - fileInfo = new FileInfo(ApplicationData.Current.LocalFolder.Path + "\\" + fileInfo.Name); + fileInfo = new FileInfo(PlatformServices.Default.Application.ApplicationBasePath + Path.DirectorySeparatorChar + fileInfo.Name); if (fileInfo.Exists) { diff --git a/Stack/Opc.Ua.Core/Types/Utils/Utils.cs b/Stack/Opc.Ua.Core/Types/Utils/Utils.cs index 7b2e4db353..5f138301b3 100644 --- a/Stack/Opc.Ua.Core/Types/Utils/Utils.cs +++ b/Stack/Opc.Ua.Core/Types/Utils/Utils.cs @@ -436,7 +436,7 @@ namespace Opc.Ua } // check for absolute path. - if (input.Length > 1 && ((input[0] == '\\' && input[1] == '\\') || input[1] == ':')) + if (Path.IsPathRooted(input)) { return input; } @@ -489,57 +489,28 @@ namespace Opc.Ua return null; } - // check installation folder. - StringBuilder buffer = new StringBuilder(); - buffer.Append("%CommonProgramFiles%\\OPC Foundation\\UA\\v1.0\\Bin\\"); - buffer.Append(fileName); + string path = null; + + // check source tree. + DirectoryInfo directory = new DirectoryInfo(PlatformServices.Default.Application.ApplicationBasePath); - string path = Utils.GetAbsoluteFilePath(buffer.ToString(), false, false, false); - - if (path == null) + while (directory != null) { - // check x86 installation folder. - path = Utils.GetAbsoluteDirectoryPath("%CommonProgramFiles%", false, false, false); + StringBuilder buffer = new StringBuilder(); + buffer.Append(directory.FullName); + buffer.Append(Path.DirectorySeparatorChar + "Bin" + Path.DirectorySeparatorChar); + buffer.Append(fileName); + + path = Utils.GetAbsoluteFilePath(buffer.ToString(), false, false, false); if (path != null) { - DirectoryInfo directory = new DirectoryInfo(path); - - buffer = new StringBuilder(); - - buffer.Append(directory.Parent.FullName); - buffer.Append(" (x86)\\"); - buffer.Append(directory.Name); - buffer.Append("\\OPC Foundation\\UA\\v1.0\\Bin\\"); - buffer.Append(fileName); - - path = Utils.GetAbsoluteFilePath(buffer.ToString(), false, false, false); + break; } + + directory = directory.Parent; } - - if (path == null) - { - // check source tree. - DirectoryInfo directory = new DirectoryInfo(ApplicationData.Current.LocalFolder.Path); - - while (directory != null) - { - buffer = new StringBuilder(); - buffer.Append(directory.FullName); - buffer.Append("\\Bin\\"); - buffer.Append(fileName); - - path = Utils.GetAbsoluteFilePath(buffer.ToString(), false, false, false); - - if (path != null) - { - break; - } - - directory = directory.Parent; - } - } - + // return what was found. return path; } @@ -564,7 +535,7 @@ namespace Opc.Ua FileInfo file = new FileInfo(filePath); // check for absolute path. - bool isAbsolute = filePath.StartsWith("\\\\", StringComparison.Ordinal) || filePath.IndexOf(':') == 1; + bool isAbsolute = Path.IsPathRooted(filePath); if (isAbsolute) { @@ -585,7 +556,7 @@ namespace Opc.Ua if (checkCurrentDirectory) { // first check in local folder - FileInfo localFile = new FileInfo(Utils.Format("{0}\\{1}", ApplicationData.Current.LocalFolder.Path, filePath)); + FileInfo localFile = new FileInfo(Utils.Format("{0}{1}{2}", PlatformServices.Default.Application.ApplicationBasePath, Path.DirectorySeparatorChar, filePath)); if (localFile.Exists) { return localFile.FullName; @@ -611,7 +582,7 @@ namespace Opc.Ua StatusCodes.BadConfigurationError, "File does not exist: {0}\r\nCurrent directory is: {1}", filePath, - ApplicationData.Current.LocalFolder.Path); + PlatformServices.Default.Application.ApplicationBasePath); } return null; @@ -670,7 +641,7 @@ namespace Opc.Ua DirectoryInfo directory = new DirectoryInfo(dirPath); // check for absolute path. - bool isAbsolute = dirPath.StartsWith("\\\\", StringComparison.Ordinal) || dirPath.IndexOf(':') == 1; + bool isAbsolute = Path.IsPathRooted(dirPath); if (isAbsolute) { @@ -693,7 +664,7 @@ namespace Opc.Ua { if (!directory.Exists) { - directory = new DirectoryInfo(Utils.Format("{0}\\{1}", ApplicationData.Current.LocalFolder.Path, dirPath)); + directory = new DirectoryInfo(Utils.Format("{0}{1}{2}", PlatformServices.Default.Application.ApplicationBasePath, Path.DirectorySeparatorChar, dirPath)); } } @@ -719,39 +690,12 @@ namespace Opc.Ua StatusCodes.BadConfigurationError, "Directory does not exist: {0}\r\nCurrent directory is: {1}", originalPath, - ApplicationData.Current.LocalFolder.Path); + PlatformServices.Default.Application.ApplicationBasePath); } return null; } - /// - /// Returns the value of the user setting. - /// - public static string GetUserSetting(string applicationName, string keyName) - { - ApplicationDataContainer settings = ApplicationData.Current.LocalSettings.CreateContainer(applicationName, ApplicationDataCreateDisposition.Always); - return settings.Values[keyName].ToString(); - } - - /// - /// Sets the value of the user setting. - /// - public static void SetUserSetting(string applicationName, string keyName, string value) - { - ApplicationDataContainer settings = ApplicationData.Current.LocalSettings.CreateContainer(applicationName, ApplicationDataCreateDisposition.Always); - settings.Values[keyName] = value; - } - - /// - /// Returns the contents of the recent file list for the application. - /// - public static List GetRecentFileList(string applicationName) - { - ApplicationDataContainer settings = ApplicationData.Current.LocalSettings.CreateContainer(applicationName, ApplicationDataCreateDisposition.Always); - return (List) settings.Values["Recent File List"]; - } - /// /// Truncates a file path so it can be displayed in a limited width view. ///