@@ -70,46 +70,33 @@ public static string GetUserName(string fileName)
7070 return trimedName ;
7171 }
7272
73- /// <summary>
74- /// Get a list of existing users from the encoded data files
75- /// </summary>
76- /// <returns>user list</returns>
77- public static List < string > GetUsersList ( )
78- {
79- List < string > userList = new List < string > ( ) ;
80-
81- var dataFiles = Directory . EnumerateFiles ( DataFilePath , PxDefs . all_xyz ) ;
82- foreach ( string currentFile in dataFiles )
83- {
84- string fileName = currentFile . Substring ( DataFilePath . Length + 1 ) ;
85- string userName = GetUserName ( fileName ) ;
86- if ( userName != string . Empty && ! string . IsNullOrWhiteSpace ( userName ) )
87- {
88- userList . Add ( userName ) ;
89- }
90- }
91- return userList ;
92- }
9373 }
9474
9575 public class User
9676 {
9777 private string _username ;
98- public string Username
78+ public string Username
9979 {
10080 get => _username ;
101- set
81+ set
10282 {
10383 _username = value ;
10484
105- // Check whether Device Lock is enabled, but key file may not exist.
106- if ( System . IO . File . Exists ( System . IO . Path . Combine ( PxDataFile . DataFilePath , GetFileName ( true ) ) ) )
85+ if ( _username == null )
10786 {
108- IsDeviceLockEnabled = true ;
87+ IsDeviceLockEnabled = false ;
10988 }
110- else
89+ else
11190 {
112- IsDeviceLockEnabled = false ;
91+ // Check whether Device Lock is enabled, but key file may not exist.
92+ if ( System . IO . File . Exists ( System . IO . Path . Combine ( PxDataFile . DataFilePath , GetFileName ( true ) ) ) )
93+ {
94+ IsDeviceLockEnabled = true ;
95+ }
96+ else
97+ {
98+ IsDeviceLockEnabled = false ;
99+ }
113100 }
114101 }
115102 }
@@ -120,7 +107,31 @@ public string Username
120107 /// Check whether Device Lock is enabled for this user.
121108 /// <c>true</c> - key file is enabled, <c>false</c> - key file is not enabled
122109 /// </summary>
123- public bool IsDeviceLockEnabled { get ; set ; }
110+ public bool IsDeviceLockEnabled { get ; set ; } = false ;
111+
112+ /// <summary>
113+ /// Check whether the key file is existed.
114+ /// true - key file is available, false - key file is not available
115+ /// </summary>
116+ public bool IsUserExist
117+ {
118+ get
119+ {
120+ if ( _username == null )
121+ {
122+ return false ;
123+ }
124+
125+ if ( System . IO . File . Exists ( Path ) )
126+ {
127+ return true ;
128+ }
129+ else
130+ {
131+ return false ;
132+ }
133+ }
134+ }
124135
125136 /// <summary>
126137 /// Check whether the key file is existed.
@@ -130,10 +141,14 @@ public bool IsKeyFileExist
130141 {
131142 get
132143 {
144+ if ( _username == null )
145+ {
146+ return false ;
147+ }
148+
133149 if ( IsDeviceLockEnabled )
134150 {
135- var keyFilePath = System . IO . Path . Combine ( PxDataFile . KeyFilePath , KeyFileName ) ;
136- if ( System . IO . File . Exists ( keyFilePath ) )
151+ if ( System . IO . File . Exists ( System . IO . Path . Combine ( PxDataFile . KeyFilePath , KeyFileName ) ) )
137152 {
138153 return true ;
139154 }
@@ -161,6 +176,10 @@ public string Path
161176 {
162177 get
163178 {
179+ if ( _username == null )
180+ {
181+ return null ;
182+ }
164183 return System . IO . Path . Combine ( PxDataFile . DataFilePath , FileName ) ;
165184 }
166185 }
@@ -173,7 +192,12 @@ public string KeyFileName
173192 {
174193 get
175194 {
176- if ( IsDeviceLockEnabled )
195+ if ( _username == null )
196+ {
197+ return string . Empty ;
198+ }
199+
200+ if ( IsDeviceLockEnabled )
177201 {
178202 return PxDefs . head_k4xyz + Base58CheckEncoding . ToBase58String ( Username ) + PxDefs . k4xyz ;
179203 }
@@ -192,6 +216,11 @@ public string KeyFileName
192216 /// <returns>Data file name</returns>
193217 private string GetFileName ( bool isDeviceLockEnabled = false )
194218 {
219+ if ( _username == null )
220+ {
221+ return null ;
222+ }
223+
195224 if ( isDeviceLockEnabled )
196225 {
197226 return PxDefs . head_data + Base58CheckEncoding . ToBase58String ( _username ) + PxDefs . xyz ;
@@ -202,6 +231,27 @@ private string GetFileName(bool isDeviceLockEnabled = false)
202231 }
203232 }
204233
234+ /// <summary>
235+ /// Get a list of existing users from the encoded data files
236+ /// </summary>
237+ /// <returns>user list</returns>
238+ public static List < string > GetUsersList ( )
239+ {
240+ List < string > userList = new List < string > ( ) ;
241+
242+ var dataFiles = Directory . EnumerateFiles ( PxDataFile . DataFilePath , PxDefs . all_xyz ) ;
243+ foreach ( string currentFile in dataFiles )
244+ {
245+ string fileName = currentFile . Substring ( PxDataFile . DataFilePath . Length + 1 ) ;
246+ string userName = PxDataFile . GetUserName ( fileName ) ;
247+ if ( userName != string . Empty && ! string . IsNullOrWhiteSpace ( userName ) )
248+ {
249+ userList . Add ( userName ) ;
250+ }
251+ }
252+ return userList ;
253+ }
254+
205255 public User ( )
206256 {
207257 IsDeviceLockEnabled = false ;
0 commit comments