C#读写共享目录

该试验分下面步骤:

1、在server设置一个共享目录。在这里我的serverip地址是10.80.88.180,共享目录名字是test,test里面有两个文件:good.txt和bad.txt,訪问权限,username是admin,password是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("未能连接。");
            }
        }
    }} 

按以上步骤。就可以实现读写网络共享文件。

C#读写共享目录的更多相关文章

  1. IIS 共享目录读写报错 Access to the path:“\\192.168.0.1\1.txt”is denied解决方案

    这个是IIS权限的问题,主要修改了以下地方,如果两台电脑有相同的用户名和密码可以跳过第一步 1.找到共享目录的文件夹,属性=>共享,给电脑创建一个新用户,共享文件下添加新用户的读写权限,然后对应 ...

  2. Linux和Linux之间共享目录

    1.Linux 服务器端NFS服务器的配置 以root身份登陆Linux服务器,编辑/etc目录下的共享目录配置文件exports,指定共享目录及权限等. 执行如下命令编辑文件/etc/exports ...

  3. 在java程序中访问windows有用户名和密码保护的共享目录

    在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0)  编辑  收藏 --> Jav ...

  4. 利用Sambaserver在Ubuntu系统和Win7系统间共享目录

    1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...

  5. Samba共享目录的多用户权限设置案例

    下面根据实际工作中遇到的一个共享目录的多用户权限需求案例来说明下Samba用户权限的设置. 一.需求场景领导:李一(liyi)正式员工(zhengshiyuangong):刘二二(liuerer).于 ...

  6. windows server 2008 R2 部署NFS,实现多台服务器间、客户端间的共享目录。

    如何通过Windows Server 2008 R2建立NFS存储服务? 通过Windows Server 2008 R2,我们可以很容易地将其作为一台NFS存储服务器,得到一个NFS软存储,轻松解决 ...

  7. 开启 NFS 文件系统提升 Vagrant 共享目录的性能

    Vagrant 默认的 VirtualBox 共享目录方式读写性能表现并不好,好在 Vagrant 支持 NFS 文件系统方式的共享,我们可以启用 NFS 提升性能 开启方法 首先要把虚拟机的网络设置 ...

  8. Docker镜像搭建Linux下samba共享目录

    Samba 是 SMB/CIFS 网络协议的重新实现, 它作为 NFS 的补充使得在 Linux.OS/2.DOS 和 Windows 系统中进行文件共享.打印机共享更容易实现.SMB协议是客户机/服 ...

  9. Linux下搭建企业共享目录方案之------samba

    Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...

随机推荐

  1. SPA SEO SSR三者有什么区别

    SPA通俗的说就是单页面应用(single page application) 优点 页面之间的切换非常快 一定程度减少了后端服务器的压力 后端程序只需要提供api,不需要客户端到底是web端还是手机 ...

  2. 向ueditor中插入内容

    html在ueditor中插入内容不能直接插入,必须判断编辑器是否创建成功,jsp可以用java代码嵌套的方式. html页面中:<textarea id="zym" nam ...

  3. STM32是如何进入中断服务函数xxx_IRQHandler的

    今天在看stm32的中断,一时间不理解stm32主函数是如何进入中断函数的,按C编程的理解,会有个特定的入口之类的,但是看demo过程中没有发现入口. 以串口中断服务函数void USART1_IRQ ...

  4. 2019年北航OO第二单元(多线程电梯任务)总结

    一.三次作业总结 1. 说在前面 对于这次的这三次电梯作业,我采用了和几乎所有人都不同的架构:将每个人当作一个线程.这样做有一定的好处:它使得整个问题的建模更加自然,并且在后期人员调度变得复杂时,可以 ...

  5. 实现图像剪裁 jquery.Jcrop

       配合 jquery.Jcrop 实现上传图片进行剪裁保存功能    <script src="js/jquery.min.js"></script> ...

  6. Android中处理Touch Icon的方案

    苹果的Touch Icon相对我们都比较熟悉,是苹果为了支持网络应用(或者说网页)添加到桌面需要的图标,有了这些Touch Icon的网页链接更加和Native应用更相像了.由于苹果设备IPod,IP ...

  7. hadoop-06-http服务

    hadoop-06-http服务 su root service httpd status service httpd stop vi /etc/httpd/conf/httpd.conf 修改:Do ...

  8. 转:iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)

    iosiOSIOS文档服务器测试电话 由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付 ...

  9. python批量下载色影无忌和蜂鸟的图片 爬虫小应用

    有些冗余信息.由于之前測试正則表達式.所以没有把它们给移走.只是不影响使用. # -*- coding:utf-8 -*- import re,urllib,sys,os,time def getAl ...

  10. 芒果TV真实视频地址解析

    本文旨在互相学习,请勿滥用 若有幸被您引用请附加地址来源http://blog.csdn.net/feige2008/article/details/37579051 文章主要解析芒果TV的视频真实地 ...