C#读写共享文件夹的方法
本文实例为大家分享了C#读写共享文件夹的具体代码,供大家参考,具体内容如下
该试验分以下步骤:
1、在服务器设置一个共享文件夹,在这里我的服务器ip地址是10.80.88.180,共享文件夹名字是test,test里面有两个文件:good.txt和bad.txt,访问权限,用户名是admin,密码是admin。
2、新建一个webapplication项目,在前台页面加一个listbox,ID是ListBox1.
3、添加后台代码如下:其中包含的功能是读文件,这里以读good文件为例;写文件,这里以写bad文件为例;还有是将test文件夹下的文件名列到listbox中。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Text;
usingSystem.Diagnostics;
usingSystem.IO;
namespaceWebApplication2
{
publicclassFileShare
{
publicFileShare(){}
publicstaticboolconnectState(stringpath)
{
returnconnectState(path,"","");
}
publicstaticboolconnectState(stringpath,stringuserName,stringpassWord)
{
boolFlag=false;
Processproc=newProcess();
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();
stringdosLine=@"netuse"+path+"/User:"+userName+""+passWord+"/PERSISTENT:YES";
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while(!proc.HasExited)
{
proc.WaitForExit(1000);
}
stringerrormsg=proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if(string.IsNullOrEmpty(errormsg))
{
Flag=true;
}
else
{
thrownewException(errormsg);
}
}
catch(Exceptionex)
{
throwex;
}
finally
{
proc.Close();
proc.Dispose();
}
returnFlag;
}
//readfile
publicstaticvoidReadFiles(stringpath)
{
try
{
//CreateaninstanceofStreamReadertoreadfromafile.
//TheusingstatementalsoclosestheStreamReader.
using(StreamReadersr=newStreamReader(path))
{
Stringline;
//Readanddisplaylinesfromthefileuntiltheendof
//thefileisreached.
while((line=sr.ReadLine())!=null)
{
Console.WriteLine(line);
}
}
}
catch(Exceptione)
{
//Lettheuserknowwhatwentwrong.
Console.WriteLine("Thefilecouldnotberead:");
Console.WriteLine(e.Message);
}
}
//writefile
publicstaticvoidWriteFiles(stringpath)
{
try
{
//CreateaninstanceofStreamWritertowritetexttoafile.
//TheusingstatementalsoclosestheStreamWriter.
using(StreamWritersw=newStreamWriter(path))
{
//Addsometexttothefile.
sw.Write("Thisisthe");
sw.WriteLine("headerforthefile.");
sw.WriteLine("-------------------");
//Arbitraryobjectscanalsobewrittentothefile.
sw.Write("Thedateis:");
sw.WriteLine(DateTime.Now);
}
}
catch(Exceptione)
{
//Lettheuserknowwhatwentwrong.
Console.WriteLine("Thefilecouldnotberead:");
Console.WriteLine(e.Message);
}
}
}
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
boolstatus=false;
//连接共享文件夹
status=FileShare.connectState(@"\\10.80.88.180\test","admin","admin");
if(status)
{
DirectoryInfotheFolder=newDirectoryInfo(@"\\10.80.88.180\test");
//先测试读文件,把目录路径与文件名连接
stringfilename=theFolder.ToString()+"\\good.txt";
FileShare.ReadFiles(filename);
//测试写文件,拼出完整的路径
filename=theFolder.ToString()+"\\bad.txt";
FileShare.WriteFiles(filename);
//遍历共享文件夹,把共享文件夹下的文件列表列到listbox
foreach(FileInfonextFileintheFolder.GetFiles())
{
ListBox1.Items.Add(nextFile.Name);
}
}
else
{
ListBox1.Items.Add("未能连接!");
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。