首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

,StreamWriter的一个有关问题

2012-03-21 
求助,StreamWriter的一个问题我用StreamWriter创建文本,并且写入文本,需要修改2个文本文件但是我用如下方

求助,StreamWriter的一个问题
我用StreamWriter创建文本,并且写入文本,需要修改2个文本文件
但是我用如下方法只能用一个,连第二个都创建不出来。
问题就是
要么创建第一个,并且可以写入数据,但是第二个无法创建无法写入数据了。
或者文件可以创建,第一个和第二个都写入不了数据
以下方法在调试里可以创建2个并且都写入数据
但是在IIS服务器里的应用程序设置里,如果不创建,就是以上的问题了
创建后问题就解决了
现在需要解决:
如何在虚拟主机里这样做。或者别的方法?
因为我需要创建3个文本并且每个文本都可以追加数据。

C# code
StreamWriter sr;                    sr = File.CreateText(Server.MapPath(str) + "\\" + DropDownList1.SelectedValue + "\\" + name + ".txt");                    sr.WriteLine("写入数据1");                    sr.Close();        using (StreamWriter sw = File.AppendText(Request.PhysicalApplicationPath +"\\" + "a.txt"))                 sw.WriteLine("写入数据");

Request.PhysicalApplicationPath应该是当前aspx文件所在目录

[解决办法]
第一个看下你在服务器上有没有权限
第二个,你参考下面的代码,最好用下面的方式来实现你要的功能
using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
}

// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);

// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}


[解决办法]
或者
C# code
using System;using System.IO;class Test {    public static void Main()     {        string path = @"c:\temp\MyTest.txt";        // This text is added only once to the file.        if (!File.Exists(path))         {            // Create a file to write to.            using (StreamWriter sw = File.CreateText(path))             {                sw.WriteLine("Hello");                sw.WriteLine("And");                sw.WriteLine("Welcome");            }            }        // This text is always added, making the file longer over time        // if it is not deleted.        using (StreamWriter sw = File.AppendText(path))         {            sw.WriteLine("This");            sw.WriteLine("is Extra");            sw.WriteLine("Text");        }            // Open the file to read from.        using (StreamReader sr = File.OpenText(path))         {            string s = "";            while ((s = sr.ReadLine()) != null)             {                Console.WriteLine(s);            }        }    }}
[解决办法]
虚拟机 IIS的权限有 写入的权限吗?、 IIS 右键 文件夹 -》 读取 ,写入 打勾
[解决办法]
你要在iis目录下有生成文件的权限。就要有写入的权限

热点排行