class
Program
{
const
int
TIMEOUT
=
10000
;
const
int
MAXREAD
=
2048
;
static
void
UpLoadFile(FtpWebRequest ftp,
string
path)
{
ftp.Method
=
WebRequestMethods.Ftp.UploadFile;
var buffer
=
new
byte
[MAXREAD];
using
(var fs
=
new
FileStream(path, FileMode.Open))
{
ftp.ContentLength
=
fs.Length;
using
(var sw
=
ftp.GetRequestStream())
{
var read
=
fs.Read(buffer,
0
, MAXREAD);
while
(read
!=
0
)
{
sw.Write(buffer,
0
, read);
read
=
fs.Read(buffer,
0
, MAXREAD);
}
}
}
}
static
FtpWebRequest FtpCreate(
string
url,
string
userName,
string
passWord)
{
var ftp
=
(FtpWebRequest)FtpWebRequest.Create(url);
ftp.Credentials
=
new
NetworkCredential(userName, passWord);
ftp.UseBinary
=
true
;
ftp.Timeout
=
TIMEOUT;
ftp.KeepAlive
=
false
;
return
ftp;
}
static
string
[] GetDirectory(
string
url,
string
userName,
string
passWord)
{
try
{
var ftp
=
FtpCreate(url, userName, passWord);
ftp.Method
=
WebRequestMethods.Ftp.ListDirectory;
using
(var sw
=
ftp.GetResponse().GetResponseStream())
{
using
(var sr
=
new
StreamReader(sw, Encoding.Default))
return
sr.ReadToEnd().Replace(
"
\r
"
,
""
).Split(
'
\n
'
);
}
}
catch
{
return
null
; }
}
static
void
Main(
string
[] args)
{
var index
=
0
;
var now
=
DateTime.Now;
Console.ForegroundColor
=
ConsoleColor.Cyan;
Console.WriteLine(
"
log_{0:yyyMMddhhmmss}.txt
"
, now);
try
{
var files
=
new
DirectoryInfo(
"
upfile
"
).GetFiles(
"
*.*
"
, SearchOption.TopDirectoryOnly);
using
(var sr
=
new
StreamReader(
"
ftp.txt
"
, Encoding.Default,
true
))
{
using
(var log
=
new
StreamWriter(
string
.Format(
"
log_{0:yyyMMddhhmmss}.txt
"
, now),
false
))
{
using
(var error
=
new
StreamWriter(
"
error.txt
"
,
false
))
{
var s
=
""
;
while
((s
=
sr.ReadLine())
!=
null
)
{
index
++
;
Console.ForegroundColor
=
ConsoleColor.Yellow;
Console.Write((
"
NO:
"
+
index.ToString()).PadRight(
8
));
var arr
=
s.Split(
'
,
'
);
var url
=
"
ftp://
"
+
arr[
0
]
+
"
/
"
;
Console.ResetColor();
Console.Write(url);
var dir
=
GetDirectory(url, arr[
1
], arr[
2
]);
if
(dir
!=
null
)
{
if
(Array.Exists(dir,
delegate
(
string
o) {
return
string
.Equals(o,
"
wwwroot
"
, StringComparison.OrdinalIgnoreCase); }))
url
+=
"
wwwroot/
"
;
else
if
(Array.Exists(dir,
delegate
(
string
o) {
return
string
.Equals(o,
"
web
"
, StringComparison.OrdinalIgnoreCase); }))
url
+=
"
web/
"
;
else
if
(Array.Exists(dir,
delegate
(
string
o) {
return
string
.Equals(o,
"
www
"
, StringComparison.OrdinalIgnoreCase); }))
url
+=
"
www/
"
;
}
try
{
foreach
(var file
in
files)
{
var ftp
=
FtpCreate(url
+
file.Name, arr[
1
], arr[
2
]);
UpLoadFile(ftp, file.FullName);
Console.ResetColor();
Console.Write(
"
\r\n...
"
.PadRight(
10
)
+
url
+
file.Name);
Console.ForegroundColor
=
ConsoleColor.Green;
Console.Write(
"
OK!
"
);
}
Console.WriteLine();
url
=
"
http://
"
+
arr[
0
]
+
"
/
"
;
log.WriteLine(url);
log.Flush();
}
catch
(Exception e)
{
error.WriteLine(
string
.Join(
"
,
"
, arr));
error.Flush();
Console.ForegroundColor
=
ConsoleColor.Red;
Console.WriteLine(
"
"
+
e.Message);
}
}
}
}
}
}
catch
(Exception e)
{
Console.ForegroundColor
=
ConsoleColor.Red;
Console.WriteLine(e.Message);
}
Console.ResetColor();
Console.WriteLine(
"
***************************************************************
"
);
Console.ForegroundColor
=
ConsoleColor.Cyan;
Console.WriteLine(
"
End!
"
);
Console.Read();
}
}