当前位置: 移动技术网 > IT编程>开发语言>c# > C#读写共享文件夹的方法

C#读写共享文件夹的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了c#读写共享文件夹的具体代码,供大家参考,具体内容如下 该试验分以下步骤: 1、在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.80.8

本文实例为大家分享了c#读写共享文件夹的具体代码,供大家参考,具体内容如下

该试验分以下步骤:

1、在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.80.88.180,共享文件夹名字是test,test里面有两个文件:good.txt和bad.txt,访问权限,用户名是admin,密码是admin。

2、新建一个webapplication项目,在前台页面加一个listbox,id是listbox1.

3、添加后台代码如下:其中包含的功能是读文件,这里以读good 文件为例;写文件,这里以写bad文件为例;还有是将test文件夹下的文件名列到listbox中。

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.text;
using system.diagnostics;
using system.io;


namespace webapplication2
{

 public class fileshare
 {
  public fileshare() { }

  public static bool connectstate(string path)
  {
   return connectstate(path,"","");
  }

  public static bool connectstate(string path,string username,string password)
   {
   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 " + path + " /user:" + username + " " + password + " /persistent: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 = true;
    }
    else
    {
     throw new exception(errormsg);
    }
   }
   catch (exception ex)
   {
    throw ex;
   }
   finally
   {
    proc.close();
    proc.dispose();
   }
   return flag;
  }


  //read file
  public static void readfiles(string path)
  {
   try
   {
    // create an instance of streamreader to read from a file.
    // the using statement also closes the streamreader.
    using (streamreader sr = new streamreader(path))
    {
     string line;
     // read and display lines from the file until the end of 
     // the file is reached.
     while ((line = sr.readline()) != null)
     {
      console.writeline(line);
      
     }
    }
   }
   catch (exception e)
   {
    // let the user know what went wrong.
    console.writeline("the file could not be read:");
    console.writeline(e.message);
   } 

  }

  //write file
  public static void writefiles(string path)
  {
   try
   {
    // create an instance of streamwriter to write text to a file.
    // the using statement also closes the streamwriter.
    using (streamwriter sw = new streamwriter(path))
    {
     // add some text to the file.
     sw.write("this is the ");
     sw.writeline("header for the file.");
     sw.writeline("-------------------");
     // arbitrary objects can also be written to the file.
     sw.write("the date is: ");
     sw.writeline(datetime.now);
    }
   }
   catch (exception e)
   {
    // let the user know what went wrong.
    console.writeline("the file could not be read:");
    console.writeline(e.message);
   }
  }
 }

 public partial class _default : system.web.ui.page
 {
  protected void page_load(object sender, eventargs e)
  {
   
   bool status = false;

   //连接共享文件夹
   status = fileshare.connectstate(@"\\10.80.88.180\test", "admin", "admin");
   if (status)
   {
    directoryinfo thefolder = new directoryinfo(@"\\10.80.88.180\test");

    //先测试读文件,把目录路径与文件名连接
    string filename = thefolder.tostring()+"\\good.txt";
    fileshare.readfiles(filename);

    //测试写文件,拼出完整的路径
    filename = thefolder.tostring() + "\\bad.txt";
    fileshare.writefiles(filename);
    
    //遍历共享文件夹,把共享文件夹下的文件列表列到listbox
    foreach (fileinfo nextfile in thefolder.getfiles())
    {
     listbox1.items.add(nextfile.name);
    }
   }
   else
   {
    listbox1.items.add("未能连接!");
   }
  }
 }
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网