Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
Ftt_Common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Ftt_Common
Commits
cb76ce99
Commit
cb76ce99
authored
May 12, 2023
by
罗显健
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化更新器脚本执行工具,解决因编码不同导致的乱码报错问题。
parent
19864606
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
10 deletions
+116
-10
Utils.cs
UpdateExecuter/Utils.cs
+104
-0
frmMain.cs
UpdateExecuter/frmMain.cs
+12
-10
No files found.
UpdateExecuter/Utils.cs
View file @
cb76ce99
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.IO
;
using
System.Text
;
namespace
UpdateExecuter
namespace
UpdateExecuter
{
{
...
@@ -140,5 +141,108 @@ namespace UpdateExecuter
...
@@ -140,5 +141,108 @@ namespace UpdateExecuter
}
}
#
region
文件编码相关
/// <summary>
/// 获取文件的编码格式
/// </summary>
public
static
Encoding
GetEncoding
(
string
fileName
)
{
FileStream
fs
=
new
FileStream
(
fileName
,
FileMode
.
Open
,
FileAccess
.
Read
);
Encoding
r
=
GetEncoding
(
fs
);
fs
.
Close
();
return
r
;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name="fs">文件流</param>
/// <returns>文件的编码类型</returns>
private
static
Encoding
GetEncoding
(
FileStream
fs
)
{
//文件的字符集在Windows下有两种,一种是ANSI,一种Unicode。
//对于Unicode,Windows支持了它的三种编码方式,一种是小尾编码(Unicode),一种是大尾编码(BigEndianUnicode),一种是UTF - 8编码。
//byte[] Unicode = new byte[] { 0xFF, 0xFE };
//byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF };
//byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //BOM头
if
(
fs
.
Length
<
3
)
return
Encoding
.
Default
;
byte
[]
bytes
=
new
byte
[
3
];
fs
.
Read
(
bytes
,
0
,
3
);
Encoding
reVal
=
Encoding
.
GetEncoding
(
"GB2312"
);
if
(
bytes
[
0
]
==
0xFE
&&
bytes
[
1
]
==
0xFF
)
{
reVal
=
Encoding
.
BigEndianUnicode
;
}
else
if
(
bytes
[
0
]
==
0xFF
&&
bytes
[
1
]
==
0xFE
)
{
reVal
=
Encoding
.
Unicode
;
}
else
{
if
(!(
bytes
[
0
]
==
0xEF
&&
bytes
[
1
]
==
0xBB
&&
bytes
[
2
]
==
0xBF
))
{
fs
.
Position
=
0
;
}
if
(
IsUTF8Bytes
(
fs
))
{
if
(
bytes
[
0
]
==
0xEF
&&
bytes
[
1
]
==
0xBB
&&
bytes
[
2
]
==
0xBF
)
reVal
=
new
UTF8Encoding
(
false
);
else
reVal
=
Encoding
.
UTF8
;
}
}
return
reVal
;
}
private
static
byte
UTF8_BYTE_MASK
=
0
b1100_0000
;
private
static
byte
UTF8_BYTE_VALID
=
0
b1000_0000
;
private
static
bool
IsUTF8Bytes
(
FileStream
fs
)
{
//BinaryReader r = new BinaryReader(fs);
byte
[]
bytes
=
new
byte
[
1
];
fs
.
Read
(
bytes
,
0
,
1
);
//1字节 0xxxxxxx
//2字节 110xxxxx 10xxxxxx
//3字节 1110xxxx 10xxxxxx 10xxxxxx
//4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
while
(
fs
.
Read
(
bytes
,
0
,
1
)
>
0
)
{
if
(
bytes
[
0
]
<
0x80
)
continue
;
int
cnt
=
0
;
byte
b
=
bytes
[
0
];
while
((
b
&
0
b1000_0000
)
!=
0
)
{
cnt
++;
b
<<=
1
;
}
cnt
-=
1
;
for
(
int
i
=
0
;
i
<
cnt
;
i
++)
{
if
(
fs
.
Read
(
bytes
,
0
,
1
)
<=
0
)
return
false
;
if
((
bytes
[
0
]
&
UTF8_BYTE_MASK
)
!=
UTF8_BYTE_VALID
)
return
false
;
}
}
return
true
;
}
#
endregion
}
}
}
}
UpdateExecuter/frmMain.cs
View file @
cb76ce99
...
@@ -117,7 +117,7 @@ namespace UpdateExecuter
...
@@ -117,7 +117,7 @@ namespace UpdateExecuter
{
{
if
(
lstClient
.
Items
[
i
].
Text
==
newClient
&&
lstClient
.
Items
[
i
]
!=
currentItem
)
if
(
lstClient
.
Items
[
i
].
Text
==
newClient
&&
lstClient
.
Items
[
i
]
!=
currentItem
)
{
{
MessageBox
.
Show
(
$"客户[
{
newClient
}
]已存在,请确认!"
);
MessageBox
.
Show
(
$"客户[
{
newClient
}
]已存在,请确认!"
);
return
false
;
return
false
;
}
}
}
}
...
@@ -166,7 +166,7 @@ namespace UpdateExecuter
...
@@ -166,7 +166,7 @@ namespace UpdateExecuter
{
{
btnExecute
.
Enabled
=
false
;
btnExecute
.
Enabled
=
false
;
Debug
.
WriteLine
(
$"当前线程:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
if
(!
CheckExecuter
())
return
;
if
(!
CheckExecuter
())
return
;
...
@@ -185,7 +185,7 @@ namespace UpdateExecuter
...
@@ -185,7 +185,7 @@ namespace UpdateExecuter
// 根据文件名排序、保证按顺序执行(脚本文件有依赖顺序)
// 根据文件名排序、保证按顺序执行(脚本文件有依赖顺序)
Array
.
Sort
(
dbFiles
,
delegate
(
FileInfo
x
,
FileInfo
y
)
{
return
x
.
FullName
.
CompareTo
(
y
.
FullName
);
});
Array
.
Sort
(
dbFiles
,
delegate
(
FileInfo
x
,
FileInfo
y
)
{
return
x
.
FullName
.
CompareTo
(
y
.
FullName
);
});
Debug
.
WriteLine
(
$"当前线程1:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程1:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
await
ExecDBScriptAsync
(
lstClientName
,
dbFiles
);
await
ExecDBScriptAsync
(
lstClientName
,
dbFiles
);
}
}
...
@@ -335,10 +335,10 @@ namespace UpdateExecuter
...
@@ -335,10 +335,10 @@ namespace UpdateExecuter
listViewItem
.
BackColor
=
System
.
Drawing
.
Color
.
Yellow
;
listViewItem
.
BackColor
=
System
.
Drawing
.
Color
.
Yellow
;
}
}
Debug
.
WriteLine
(
$"当前线程4:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程4:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
}));
}));
Debug
.
WriteLine
(
$"当前线程5:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程5:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
}
}
}
}
});
});
...
@@ -381,7 +381,9 @@ namespace UpdateExecuter
...
@@ -381,7 +381,9 @@ namespace UpdateExecuter
List
<
string
>
lstSql
=
null
;
List
<
string
>
lstSql
=
null
;
if
(!
conDicFileScript
.
ContainsKey
(
file
.
FullName
))
if
(!
conDicFileScript
.
ContainsKey
(
file
.
FullName
))
{
{
lstSql
=
Utils
.
SliceSingleFileScript
(
File
.
ReadAllText
(
file
.
FullName
,
System
.
Text
.
Encoding
.
Default
));
var
encoding
=
Utils
.
GetEncoding
(
file
.
FullName
);
var
strContent
=
File
.
ReadAllText
(
file
.
FullName
,
encoding
);
lstSql
=
Utils
.
SliceSingleFileScript
(
strContent
);
conDicFileScript
.
TryAdd
(
file
.
FullName
,
lstSql
);
conDicFileScript
.
TryAdd
(
file
.
FullName
,
lstSql
);
}
}
else
else
...
@@ -432,11 +434,11 @@ namespace UpdateExecuter
...
@@ -432,11 +434,11 @@ namespace UpdateExecuter
{
{
string
clientName
=
client
.
ClientName
;
string
clientName
=
client
.
ClientName
;
Debug
.
WriteLine
(
$"当前线程2:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程2:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
await
Task
.
Run
<
bool
>(()
=>
await
Task
.
Run
<
bool
>(()
=>
{
{
Debug
.
WriteLine
(
$"当前线程3:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程3:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
var
result
=
UploadUpdateFile
(
localUpdatePath
,
client
);
var
result
=
UploadUpdateFile
(
localUpdatePath
,
client
);
...
@@ -453,13 +455,13 @@ namespace UpdateExecuter
...
@@ -453,13 +455,13 @@ namespace UpdateExecuter
listViewItem
.
BackColor
=
System
.
Drawing
.
Color
.
Yellow
;
listViewItem
.
BackColor
=
System
.
Drawing
.
Color
.
Yellow
;
}
}
Debug
.
WriteLine
(
$"当前线程4:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程4:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
}));
}));
return
result
;
return
result
;
});
});
Debug
.
WriteLine
(
$"当前线程5:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
Debug
.
WriteLine
(
$"当前线程5:
{
System
.
Threading
.
Thread
.
CurrentThread
.
ManagedThreadId
}
"
);
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment