C#读写局域⽹共享⽂件夹⽂件
1、在服务器设置⼀个共享⽂件夹,在这⾥我的服务器ip地址是10.200.8.73,共享⽂件夹名字是share,访问权限,⽤户名是administrator,密码是11111111。
2、新建⼀个控制台项⽬。
3、添加后台代码如下:
class Program
{
static void Main(string[] args)
{
bool status = false;
//连接共享⽂件夹
status = connectState(@"\\10.200.8.73\share", "administrator", "11111111");
if (status)
{
//共享⽂件夹的⽬录
DirectoryInfo theFolder = new DirectoryInfo(@"\\10.200.8.73\share");
//相对共享⽂件夹的路径
string fielpath=@"\123\456\";
//获取保存⽂件的路径
string filename = theFolder.ToString() +fielpath ;
//执⾏⽅法
Transport(@"D:\1.jpg", filename, "1.jpg");
}
else
{
//ListBox1.Items.Add("未能连接!");
}
Console.ReadKey();
}
国外有名医学院public static bool connectState(string path)
{
return connectState(path, "", "");
}
///<summary>
/
//连接远程共享⽂件夹
///</summary>
///<param name="path">远程共享⽂件夹的路径</param>
///<param name="userName">⽤户名</param>
///<param name="passWord">密码</param>
///<returns></returns>
public static bool connectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "";
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 " + path + "" + passWord + " /user:" + userName;
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;
}
finally
{
proc.Close();
proc.Dispose();
支付宝基金怎么取出来}
return Flag;带草字头的字
}
///<summary>
///向远程⽂件夹保存本地内容,或者从远程⽂件夹下载⽂件到本地
///</summary>
///<param name="src">要保存的⽂件的路径,如果保存⽂件到共享⽂件夹,这个路径就是本地⽂件路径如:@"D:\1.avi"</param> ///<param name="dst">保存⽂件的路径,不含名称及扩展名</param>
///<param name="fileName">保存⽂件的名称以及扩展名</param>
public static void Transport(string src, string dst,string fileName)
{祝老师元旦快乐的祝福语简短
FileStream inFileStream = new FileStream(src, FileMode.Open);
if (!Directory.Exists(dst))
{
银行从业人员资格证Directory.CreateDirectory(dst);
}
dst = dst + fileName;
FileStream outFileStream = new FileStream(dst, 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();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论