88using Java . Lang ;
99using System . Diagnostics . Metrics ;
1010using System . Security . AccessControl ;
11+ using System . Security . Principal ;
1112using System . Xml . Linq ;
1213
1314namespace datawedge_MAUI_SampleApp ;
@@ -18,7 +19,8 @@ public class MainActivity : MauiAppCompatActivity
1819{
1920 //protected override void OnCreate(Bundle savedInstanceState) {
2021 protected override void OnPostCreate ( Bundle savedInstanceState )
21- {
22+ {
23+ ImportProfile ( "dwprofile_com.ndzl.dwmaui" ) ;
2224 base . OnPostCreate ( savedInstanceState ) ;
2325 RegisterReceivers ( ) ;
2426 WeakReferenceMessenger . Default . Send ( DisplayDotNetVersion ( ) ) ;
@@ -99,4 +101,80 @@ void RegisterReceivers()
99101
100102 Intent regres = AndroidX . Core . Content . ContextCompat . RegisterReceiver ( this , new DWIntentReceiver ( ) , filter , AndroidX . Core . Content . ContextCompat . ReceiverExported ) ;
101103 }
104+
105+ private void ImportProfile ( string profileFilenameWithoutDbExtension )
106+ {
107+ // Define directories and file names
108+ // /enterprise/device/settings/datawedge/autoimport
109+ string autoImportDir = "/enterprise/device/settings/datawedge/autoimport/" ;
110+ string temporaryFileName = profileFilenameWithoutDbExtension + ".tmp" ;
111+ string finalFileName = profileFilenameWithoutDbExtension + ".db" ;
112+
113+ Stream inputStream = null ;
114+ FileStream fileOutputStream = null ;
115+ FileInfo outputFile = null ;
116+ FileInfo finalFile = null ;
117+
118+ try
119+ {
120+ // Access the asset stream from the application assets
121+ inputStream = FileSystem . Current . OpenAppPackageFileAsync ( finalFileName ) . Result ;
122+
123+ // Create a directory for the output if it doesn't exist
124+ DirectoryInfo outputDirectory = new DirectoryInfo ( autoImportDir ) ;
125+ if ( ! outputDirectory . Exists )
126+ {
127+ outputDirectory . Create ( ) ;
128+ }
129+
130+ // Create temporary and final file objects
131+ outputFile = new FileInfo ( Path . Combine ( outputDirectory . FullName , temporaryFileName ) ) ;
132+ finalFile = new FileInfo ( Path . Combine ( outputDirectory . FullName , finalFileName ) ) ;
133+
134+ // Create a FileStream for the temporary output file
135+ fileOutputStream = new FileStream ( outputFile . FullName , FileMode . Create , FileAccess . Write ) ;
136+
137+ // Transfer bytes from the input stream to the output stream
138+ byte [ ] buffer = new byte [ 1024 ] ;
139+ int length ;
140+ int tot = 0 ;
141+ while ( ( length = inputStream . Read ( buffer , 0 , buffer . Length ) ) > 0 )
142+ {
143+ fileOutputStream . Write ( buffer , 0 , length ) ;
144+ tot += length ;
145+ }
146+ Console . WriteLine ( $ "{ tot } bytes copied") ;
147+
148+ // Flush and close the output stream
149+ fileOutputStream . Flush ( ) ;
150+ }
151+ catch ( Java . Lang . Exception e )
152+ {
153+ Console . WriteLine ( e . ToString ( ) ) ;
154+ }
155+ finally
156+ {
157+ // Ensure the stream is properly closed
158+ fileOutputStream ? . Close ( ) ;
159+
160+ // Set permissions and rename the file if applicable
161+ if ( outputFile != null )
162+ {
163+ // Rename the temporary file to the final file
164+ if ( finalFile != null )
165+ {
166+ if ( outputFile . Exists )
167+ {
168+ if ( finalFile . Exists )
169+ {
170+ finalFile . Delete ( ) ;
171+ }
172+
173+ FilePermissionHelper . setFilePermissions ( outputFile . FullName , true , true , true ) ;
174+ outputFile . MoveTo ( finalFile . FullName ) ;
175+ }
176+ }
177+ }
178+ }
179+ }
102180}
0 commit comments