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,信息服务块)是一种在局域网上共享文件和打印机的一种通 ...
随机推荐
- IE9 下的ajax缓存问题的处理
使用jQuery的getJSON从后台定时获取数据并刷新界面,使用以下方法时,在Chrome,Firefox下没问题,但在IE9下却无法刷新数据 1 2 3 4 5 $.getJSON(webAp ...
- MySQL 数据还原
1.1还原使用mysqldump命令备份的数据库的语法如下: mysql -u root -p [dbname] < backup.sq 示例: mysql -u root -p < C: ...
- P1064 金明的预算方案 (依赖性背包问题)
这道题可以用分组背包来做. 但是分组有两种方式 一种是把主件,主件+附件1,主件+附件2分成一组 组内只能选一个物品 一种是建一颗树,用树形dp的方式去做 第二种更通用,就算物品的依赖关系是森林都可以 ...
- readb(), readw(), readl(),writeb(), writew(), writel() 宏函数
参见: http://blog.csdn.net/hustyangju/article/details/20448339
- Qt之QCryptographicHash
简述 QCryptographicHash类提供了生成密码散列的方法.该类可以用于生成二进制或文本数据的加密散列值.目前支持MD4.MD5.SHA-1.SHA-224.SHA-256.SHA-384和 ...
- SpringMVC+Jquery -页面异步载入数据
背景: 做项目时涉及到页面.当我打算在controller中传一个list到页面,然后通过<c:foreach>循环遍历出来时,同事说:你这样每次都要刷新.这都是几百年前使用的技术了.你用 ...
- c++ string类的完整实现!!!
本文实现了c++ STL中的basic_string模板类,当然.通过typedef也就实现了string类和wstring类.限于篇幅,实现代码中用到了标准库的char_traits模板类,本人自己 ...
- LightOJ - 1132 Summing up Powers 矩阵高速幂
题目大意:求(1^K + 2^K + 3K + - + N^K) % 2^32 解题思路: 借用别人的图 能够先打表,求出Cnm,用杨辉三角能够高速得到 #include<cstdio> ...
- mysql-总结select各子句及其顺序
顺序:from->where ->group by->having ->order by
- 基于IBM Bluemix的数据缓存应用实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:IBM® Data Cache for Bluemix 是快速缓存服务.支持 Web 和 ...