1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > unity 局域网读取文件_C#读写局域网共享文件夹文件

unity 局域网读取文件_C#读写局域网共享文件夹文件

时间:2023-11-22 05:11:05

相关推荐

unity 局域网读取文件_C#读写局域网共享文件夹文件

classProgram

{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(stringpath)

{return connectState(path, "", "");

}///

///连接远程共享文件夹///

/// 远程共享文件夹的路径

/// 用户名

/// 密码

///

public static bool connectState(string path, string userName, stringpassWord)

{bool Flag = false;

Process proc= newProcess();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" + 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 newException(errormsg);

}

}catch(Exception ex)

{throwex;

}finally{

proc.Close();

proc.Dispose();

}returnFlag;

}///

///向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地///

/// 要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"

/// 保存文件的路径,不含名称及扩展名

/// 保存文件的名称以及扩展名

public static void Transport(string src, string dst,stringfileName)

{

FileStream inFileStream= newFileStream(src, FileMode.Open);if (!Directory.Exists(dst))

{

Directory.CreateDirectory(dst);

}

dst= dst +fileName;

FileStream outFileStream= newFileStream(dst, FileMode.OpenOrCreate);byte[] buf = new byte[inFileStream.Length];intbyteCount;while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)

{

outFileStream.Write(buf,0, byteCount);

}

inFileStream.Flush();

inFileStream.Close();

outFileStream.Flush();

outFileStream.Close();

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。