这样直接访问不行吗? Dim fileContents As String fileContents = My.Computer.FileSystem.ReadAllText(Server.MapPath("") & "\1.txt") if fileContents="1" then My.Computer.FileSystem.WriteAllText(Server.MapPath("") & "/1.txt", "0", False)
没有十分明白你的意思,看看是否符合你的要求。 try{ String path = "c:\\file.txt"; int num = ReadNum(path); if( num<0 ){ //获得文本异常 // End... } if( num>0 ) { CreateFile(0.ToString(), path); } else if(num==0 ) { Response.End(); } }catch(Exception ex){ Response.Write(ex.ToString()); } public static int ReadNum(String path){ using(System.IO.StreamReader sr = new System.IO.StreamReader(path)){ int num = -1; try{ num = int.Parse(sr.ReadToEnd().Trim()); }catch(Exception ex){ throw new Exception(ex.ToString()); }finally{ sr.Close(); } } return num; } } public static void CreateFile( String str, String path) { System.IO.StreamWriter objSW = null; try{ objSW = new System.IO.StreamWriter( path); objSW.Write(str); objSW.Flush(); objSW.Close(); }catch( Exception ex ){ throw new Exception(ex.ToString()); } finally {
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Server.MapPath("") & "\1.txt")
if fileContents="1" then My.Computer.FileSystem.WriteAllText(Server.MapPath("") & "/1.txt", "0", False)
try{
String path = "c:\\file.txt";
int num = ReadNum(path);
if( num<0 ){
//获得文本异常
// End...
}
if( num>0 ) {
CreateFile(0.ToString(), path);
} else if(num==0 ) {
Response.End();
}
}catch(Exception ex){
Response.Write(ex.ToString());
}
public static int ReadNum(String path){
using(System.IO.StreamReader sr = new System.IO.StreamReader(path)){
int num = -1;
try{
num = int.Parse(sr.ReadToEnd().Trim());
}catch(Exception ex){
throw new Exception(ex.ToString());
}finally{
sr.Close();
}
}
return num;
}
}
public static void CreateFile( String str, String path)
{
System.IO.StreamWriter objSW = null;
try{
objSW = new System.IO.StreamWriter( path);
objSW.Write(str);
objSW.Flush();
objSW.Close();
}catch( Exception ex ){
throw new Exception(ex.ToString());
}
finally
{
if( objSW != null ){
objSW.Close();
objSW = null;
}
}
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
// 读取文件的内容。
Response.Write("Reading file <br>");
ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}