C#读写共享目录
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#读写共享目录的更多相关文章
- IIS 共享目录读写报错 Access to the path:“\\192.168.0.1\1.txt”is denied解决方案
这个是IIS权限的问题,主要修改了以下地方,如果两台电脑有相同的用户名和密码可以跳过第一步 1.找到共享目录的文件夹,属性=>共享,给电脑创建一个新用户,共享文件下添加新用户的读写权限,然后对应 ...
- Linux和Linux之间共享目录
1.Linux 服务器端NFS服务器的配置 以root身份登陆Linux服务器,编辑/etc目录下的共享目录配置文件exports,指定共享目录及权限等. 执行如下命令编辑文件/etc/exports ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...
- 利用Sambaserver在Ubuntu系统和Win7系统间共享目录
1 介绍 如今是网络化的时代,我们每一个人要更好的发展.离不开网络化.信息化的支持.利用网络的支持.在不同的操作系统间共享文件等信息,是计算机专业学生必备的一项技能. 本文所讲的就是怎样建立.设置.链 ...
- Samba共享目录的多用户权限设置案例
下面根据实际工作中遇到的一个共享目录的多用户权限需求案例来说明下Samba用户权限的设置. 一.需求场景领导:李一(liyi)正式员工(zhengshiyuangong):刘二二(liuerer).于 ...
- windows server 2008 R2 部署NFS,实现多台服务器间、客户端间的共享目录。
如何通过Windows Server 2008 R2建立NFS存储服务? 通过Windows Server 2008 R2,我们可以很容易地将其作为一台NFS存储服务器,得到一个NFS软存储,轻松解决 ...
- 开启 NFS 文件系统提升 Vagrant 共享目录的性能
Vagrant 默认的 VirtualBox 共享目录方式读写性能表现并不好,好在 Vagrant 支持 NFS 文件系统方式的共享,我们可以启用 NFS 提升性能 开启方法 首先要把虚拟机的网络设置 ...
- Docker镜像搭建Linux下samba共享目录
Samba 是 SMB/CIFS 网络协议的重新实现, 它作为 NFS 的补充使得在 Linux.OS/2.DOS 和 Windows 系统中进行文件共享.打印机共享更容易实现.SMB协议是客户机/服 ...
- Linux下搭建企业共享目录方案之------samba
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...
随机推荐
- 学习参考:《Python语言及其应用》中文PDF+英文PDF+代码
学习简单的数据类型,以及基本的数学和文本操作,学习用Python内置的数据结构来处理数据: 掌握Python的代码结构和函数的用法:使用模块和包编写大规模Python程序:深入理解对象.类和其他面向对 ...
- 管理windows自启动程序
1. 点击开始,在运行程序框中输入msconfig,然后回车. 在弹出的对话框中,点击”启动“选项卡,在启动项目列表中,把不需要的启动项目前面的对号去掉. 然后切换到”服务“选项卡,这里的服务项目列表 ...
- 微信小程序 上传图的功能
首先选择图片,然后循环,再就是在点击发布的时候循环图片地址赋值,包括删除命令 js代码: //选择图片 uploadImgAdd: function(e) { var imgs = this.data ...
- (50)与magento集成
我对接的是 odoo8 和 magento1.9.x 准备工作: l 服务器 装上mangento 组件 : $ pip install magento 装上 requests 组件:$ pip ...
- iOS基础UI控件介绍-Swift版
iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- Spring的控制反转(IOC)和依赖注入(DI)具体解释
Spring的控制反转(IOC)和依赖注入(DI)具体解释 首先介绍下(IOC)控制反转: 所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护是由外部容器负责的.这样控制器就有应 ...
- Linux之我最常用的20条命令
Linux之我最常用的20条命令 玩过 Linux的人都会知道, Linux中的命令的确是非常多,但是玩过 Linux的人也从来不会因为 Linux的命令如此之多而烦恼,因为我们只需要掌握我们最常用的 ...
- 里根上台时国债只占GDP的30%
学里根是刻舟求剑,关键是钱从哪来 5 里根主要靠借钱,这是冷战红利,美国打完二战国债占了GDP的120%,然后总量就没怎么增加,但战后GDP快速增长,结果国债占GDP的比例连续下降,打越战登月石油危 ...
- DNS解析污染原理——要么修改包,要么直接丢弃你的网络包
DNS/域名解析 可以看到dns解析是最初的一步,也是最重要的一步.比如访问亲友,要知道他的正确的住址,才能正确地上门拜访. dns有两种协议,一种是UDP(默认),一种是TCP. udp 方式,先回 ...