这篇文章主要介绍了C#怎么把文件上传到服务器中的指定地址的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C#怎么把文件上传到服务器中的指定地址文章都会有所收获,下面我们一起来看看吧。
public string connectFTP(string vPath, string vUID, string vPassword)
{
string errormsg = "";
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
}
catch (Exception ex)
{
//throw ex;
//MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return errormsg;
}
public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
{
bool status = false;
status = connectState(vPath, vUID, vPassword);
if (status)
{
DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
string filename = vLocalPath;
Transport(vLocalPath, vPath + "/" + file);
//System.Diagnostics.Process.Start(vPath);
}
else
{
mesLog.Info("未能连接!");
//MessageBox.Show("未能连接!");
}
}
public static bool connectState(string vPath, string vUID, string vPassword)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
//throw ex;
//MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
public static void Transport(string src, string fileName)
{
FileStream inFileStream = new FileStream(src, FileMode.Open);
FileStream outFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
byte[] buf = new byte[inFileStream.Length];
int byteCount;
while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
{
outFileStream.Write(buf, 0, byteCount);
}
inFileStream.Flush();
inFileStream.Close();
outFileStream.Flush();
outFileStream.Close();
File.Delete(src);
}
关于“C#怎么把文件上传到服务器中的指定地址”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“C#怎么把文件上传到服务器中的指定地址”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。