该试验分以下步骤:

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();
}
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("未能连接!");
}
}
}}

转自:http://www.cnblogs.com/ManMonth/archive/2011/10/11/2206998.html

C#读写共享文件夹的更多相关文章

  1. Java 使用jcifs读写共享文件夹报错jcifs.smb.SmbException: Failed to connect: 0.0.0.0<00>/10.1.*.*

    Q:使用jcifs读写Windows 10 共享文件夹中的文件报jcifs.smb.SmbException: Failed to connect: 0.0.0.0<00>/10.1.*. ...

  2. 在Java程序中读写windows共享文件夹

    摘要 使用Java通过JCIFS框架读写共享文件夹,使用SMB协议,并支持域认证. 项目常常需要有访问共享文件夹的需求,例如读取共享文件夹存储的视频.照片和PPT等文件.那么如何使用Java读写Win ...

  3. 实战ASP.NET访问共享文件夹(含详细操作步骤)

    博客园找找看(http://zzk.cnblogs.com)的索引文件占用空间太大,需要移至另外一台服务器,所以要解决"在ASP.NET中通过共享文件夹访问索引文件"的问题. 假设 ...

  4. Java读写Windows共享文件夹 .

    版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...

  5. [转]C#读写远程共享文件夹

    1.在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.200.8.73,共享文件夹名字是share,访问权限,用户名是administrator,密码是11111111. 2.新建一个控制台 ...

  6. linux基础3——与XP共享文件夹的设置

    导出linux文件有一般有三种方式: 1.类似windows下的文件直接拖拽,鼠标选中目标文件,从linux文件目录下直接拉至windows文件目录下: 2.U盘拷贝导出到Windows文件目录下:同 ...

  7. [Asp.net]通过uploadify将文件上传到B服务器的共享文件夹中

    写在前面 客户有这样的一个需求,针对项目中文档共享的模块,客户提出如果用户上传特别的大,或者时间久了硬盘空间就会吃满,能不能将这些文件上传到其他的服务器?然后就稍微研究了下这方面的东西,上传到网络中的 ...

  8. 烂泥:CentOS6.5挂载windows共享文件夹

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 由于工作需要,需要把本机的文件夹共享出去,然后让CentOS服务器临时使用下. 服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使 ...

  9. Linux下通过NFS共享文件夹

    测试环境:CentOS 6.7 服务端 # yum -y install nfs-utils rpcbind # 开启服务 service nfs start service rpcbind star ...

随机推荐

  1. HDOJ 2037简单的贪心算法

    代码: #include<iostream> using namespace std; int main() { int n,s,t1[100],t2[100],i,t,j; while( ...

  2. Codeforces 193D Two Segments 解题报告

    先是在蓝桥杯的网站上看到一道题: 给出1~n的一个排列,求出区间内所有数是连续自然数的区间的个数.n<=50000. 由于数据较弱,即使用O(N^2)的算法也能拿到满分. 于是在CF上发现了这一 ...

  3. Python下调用json.dumps中文显示问题解决办法

    json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": & ...

  4. 关于javascript输出中文乱码的问题

    今天找到一个引导效果.原来是用英文进行引导.但是我改了里面的英文为汉字就出现乱码的情况.英文提示是在js页面里面完成的.所以最后的解决办法 就是把js文件用记事本打开,然后把文件另存为utf-8的格式 ...

  5. Bootstrap_Javascript_手风琴

    触发手风琴可以通过自定义的 data-toggle 属性来触发.其中data-toggle值设置为 collapse,data-target="#折叠区标识符". 第一步,设计一个 ...

  6. 生成订单唯一id

    $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'); $orderSn = $yCode[intval(date('Y') ...

  7. ASP.NET MVC轻教程 Step By Step 2 ——View初探

    在上一节我们完成了一个最简化的MVC程序,最重要的是下面这段代码. public class HomeController : Controller { public string Index() { ...

  8. java 知识结构

    JAVA基础阶段 阶段 技术名称 技术内容 T线 JavaSE JAVA 开发基础知识 | Eclipse 开发环境 | JavaSE 7.0 API | JavaSE 8.0新特性 | 多线程技术 ...

  9. Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一)

    Solr4.8.0源码分析(20)之SolrCloud的Recovery策略(一) 题记: 我们在使用SolrCloud中会经常发现会有备份的shard出现状态Recoverying,这就表明Solr ...

  10. nutch,hbase,zookeeper兼容性问题

    nutch-2.1使用gora-0.2.1, gora-0.2.1使用hbase-0.90.4,hbase-0.90.4和hadoop-1.1.1不兼容,hbase-0.94.4和gora-0.2.1 ...