public static void Main() { string path = @"c:\temp\MyTest.txt"; FileInfo fi1 = new FileInfo(path); if (!fi1.Exists) { //Create a file to write to. using (StreamWriter sw = fi1.CreateText()) { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } } //Open the file to read from. using (StreamReader sr = fi1.OpenText()) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } try { string path2 = path + "temp"; FileInfo fi2 = new FileInfo(path2); //Ensure that the target does not exist. fi2.Delete(); //Copy the file. fi1.CopyTo(path2); Console.WriteLine("{0} was copied to {1}.", path, path2); //Delete the newly created file. fi2.Delete(); Console.WriteLine("{0} was successfully deleted.", path2); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
请不要出现"服务端"/"客户端"/"Socket"之类的
try catch
httpwebrquest
webclient
1、如果这个网络文件夹是通过ftp方式对外开放的,你可以通过ftp操作,去访问,来判断文件夹是否存在,存在后就可以上传文件(写入)
2、如果这个网络文件夹是通过网络共享的方式开放的,你应该就可以通过创建一个FileInfo指向它,去判断。
using System.IO;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
FileInfo fi1 = new FileInfo(path); if (!fi1.Exists)
{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
} //Open the file to read from.
using (StreamReader sr = fi1.OpenText())
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
} try
{
string path2 = path + "temp";
FileInfo fi2 = new FileInfo(path2); //Ensure that the target does not exist.
fi2.Delete(); //Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2); //Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2); }
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}