(转)C#读写共享文件
原文地址:https://www.cnblogs.com/guohu/p/4694515.html
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("未能连接!");
}
}
}}
上面园友的代码实现共享文件的读写,但是实际实用中遇到一点问题,提示“不允许一个用户使用一个以上用户名与一个服务器或共享资源的多重连接。。。”。后来发现是在打开共享文件目录的情况下,运行程序就会出错,此时应该是运行了两次登录吧?!(开放环境 VS2013+Win7 64)
解决方法:打开cmd命令窗口:
1.输入命令:net use查看你当前与网络资源的连接,例如驱动器映射、IPC连接。
2.用net use * /del /y命令中断开所有连接.
参考资料:https://blog.csdn.net/liuhaomatou/article/details/39395209
(转)C#读写共享文件的更多相关文章
- 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.*. ...
- C#读写共享文件夹
该试验分以下步骤: 1.在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.80.88.180,共享文件夹名字是test,test里面有两个文件:good.txt和bad.txt,访问权限,用 ...
- 在Java程序中读写windows共享文件夹
摘要 使用Java通过JCIFS框架读写共享文件夹,使用SMB协议,并支持域认证. 项目常常需要有访问共享文件夹的需求,例如读取共享文件夹存储的视频.照片和PPT等文件.那么如何使用Java读写Win ...
- 实战ASP.NET访问共享文件夹(含详细操作步骤)
博客园找找看(http://zzk.cnblogs.com)的索引文件占用空间太大,需要移至另外一台服务器,所以要解决"在ASP.NET中通过共享文件夹访问索引文件"的问题. 假设 ...
- linux服务之svn
架构:c/s 开发语言:python 服务器端:在linux平台下部署 客户端:分gui与cli两种操作界面 相关包: http://blog.sina.com.cn/s/blog_53b95aec0 ...
- 代码发布架构方案(SVN)
问题: 安装优化软件环境nginx,lvs 程序代码(不断更新) 配置更新(不断变更) 1.SVN介绍 1.1 什么是SVN(Subversion)? SVN(Subversion) ...
- 版本控制系统-SVN(1)
1. SVN介绍 1.1. 简介 SVN(subversion),版本管理工具,与CVS一样,SVN是一个可跨平台的开源版本控制系统,Subversion管理随时间变化的数据.这些数据都被放置在 ...
- java多线程同步机制
一.关键字: thread(线程).thread-safe(线程安全).intercurrent(并发的) synchronized(同步的).asynchronized(异步的). volatile ...
- VC++6.0远程调试(亲试成功)
0 前言 VS2008及以上远程调试上篇已经讲过,这里再讲下VC++6.0开发环境下的远程调试能力,仅需下面4步即可,更方便的就接着后面的5-6步. 因为目标程序需要在有采集卡等相关硬件支持下的工控机 ...
随机推荐
- TS学习之for..of
for..of会遍历可迭代的对象,调用对象上的Symbol.iterator方法(可迭代对象,数组,字符串等) let arr = ["hello", "ts" ...
- e生保plus
e生保plus https://m.health.pingan.com/share/products/esb_plus.html?re_from=qdlmMSDbxtj&order_from= ...
- Databinding in WPF
https://www.codeproject.com/Articles/680271/Simplest-MVVM-Ever
- mysql之 表数据存放路径非datadir目录
假如,新建一张表,并让该表的存储路径 不是默认的/path/to/datadir/dbname .而是 指定存储的位置 应该如何处理? 方法一shell> mkdir /Generalt1she ...
- amqp 和 exchange 详细解释
amqp 的 excange 字面意思是一个交换机.他的任务是吧 消息 分配给消息队列. amqp 的 exchange 有三种,分别是 Direct , fanout 和 toppic.三种. ...
- Webstorm/IntelliJ Idea 过期破解方法
一.Webstorm过期破解方法 如下图,WebStorm过期了,每次都是用30分钟,重新打开. 解决方法: 注册时,在打开的License Activation窗口中选择“License serve ...
- piwik custom variables
piwik custom variables 是一个功能非常强大的自定义变量跟踪方案,多用于基于访客或是页面级别的变量跟踪.piwik默认最多可以添加5个自定义变量. 使用方式是在客户端脚本里添加如 ...
- delphi中Time消息的使用方法
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 美剧黑名单的插曲《Jolene》
网易上有Slowdown 版本.<Jolene>Dolly Parton
- Action<T> Delegate
来源:https://docs.microsoft.com/zh-cn/dotnet/api/system.action-1?view=netframework-4.7.2 Action<T&g ...