介绍
怎么在c#中读写共享文件夹?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
1,在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.80.88.180,共享文件夹名字是测试,测试里面有两个文件:good.txt和bad.txt,访问权限,用户名是管理员,密码是admin。
2,新建一个webapplication项目,在前台页面加一个列表框,ID是ListBox1。
3,添加后台代码如下:其中包含的功能是读文件,这里以读好的文件为例,写文件,这里以写坏文件为例,还有是将测试文件夹下的文件名列到列表框中。
using 系统; using System.Collections.Generic; using 来; using 包含; using System.Web.UI; using System.Web.UI.WebControls; using 包含; using System.Diagnostics; using 先; namespace WebApplication2 { public class 文件共享 ,{ public 才能;文件共享(),{,} public 才能static bool  connectState (string 路径) {才能 ,,return connectState(路径,““,““); ,,} public 才能static bool  connectState (string  string  string 路径;用户名,密码) ,,{ ,,bool Flag =,假; ,,Process proc =, new 过程(); ,,试一试 ,,{ ,,,proc.StartInfo.FileName =,“cmd.exe"; ,,,proc.StartInfo.UseShellExecute =,假; ,,,proc.StartInfo.RedirectStandardInput =,真的; ,,,proc.StartInfo.RedirectStandardOutput=true; ,,,proc.StartInfo.RedirectStandardError=true; ,,,proc.StartInfo.CreateNoWindow=true; ,,,proc.Start (); ,,,string dosLine =, @" net use “, +, path +,“,/用户:“,+,userName +,,,,, +, passWord +,“,/持久:YES"; ,,,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 =,真的; ,,,} ,,, ,,,{ ,,,,throw new 例外(errormsg); ,,,} ,,} ,,catch (Exception 交货) ,,{ ,,,throw 交货; ,,} 最后,, ,,{ ,,,proc.Close (); ,,,proc.Dispose (); ,,} ,,return 旗帜; ,,} ,//read 文件 public 才能static void  ReadFiles (string 路径) {才能 ,,试一试 ,,{ ,,,//,Create an instance of StreamReader 用read 得到a 文件。 ,,,//,,using statement also closes 从而StreamReader。 ,,,using (StreamReader sr =, new StreamReader(路径)) ,,,{ ,,,,String 行; ,,,,//,Read 以及display lines 得到,file until 从而最终获得of ,,,,//,,file is 达成。 ,,,,while ((=line sr.ReadLine ()), !=, null) ,,,,{ ,,,,,Console.WriteLine(线); ,,,,, ,,,,} ,,,} ,,} ,,catch (Exception e) ,,{ ,,,//,Let 从而user 知道what went 错误的。 ,,,Console.WriteLine (“, file could not be 写道:“); ,,,Console.WriteLine (e.Message); ,,}, ,,} ,//write 文件 public 才能static void  writefile (string 路径) {才能 ,,试一试 ,,{ ,,,//,Create an instance of StreamWriter 用write text 用a 文件。 ,,,//,,using statement also closes 从而StreamWriter。 ,,,using (StreamWriter sw =, new StreamWriter(路径)) ,,,{ null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么在c#中读写共享文件夹