C# 如何指定Streamwriter路径位置
我想用StreamWriter将文本写入文件,但文件名应该是当前日期名。 这是我的代码。有人能告诉我如何指定文件创建路径吗 代码编辑: 在这里我想创建一个.txt文件,但在这里没有创建文件C# 如何指定Streamwriter路径位置,c#,c#-4.0,C#,C# 4.0,我想用StreamWriter将文本写入文件,但文件名应该是当前日期名。 这是我的代码。有人能告诉我如何指定文件创建路径吗 代码编辑: 在这里我想创建一个.txt文件,但在这里没有创建文件 public void WriteToFile( string name, string source, int dest, string messageIn, string operatorNew) { string directory = ResolveUrl("~/DesktopModules/S
public void WriteToFile( string name, string source, int dest, string messageIn, string operatorNew)
{
string directory = ResolveUrl("~/DesktopModules/SMSFunction/SMSText");
string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now,name);
string path = Path.Combine(directory, filename);
if (!File.Exists(filename))
{
using (StreamWriter str = File.CreateText(path))
{
str.WriteLine("msisdn: " + source);
str.WriteLine("shortcode : " + dest);
str.WriteLine("Message : " + messageIn);
str.WriteLine("Operator :" + operatorNew);
str.Flush();
}
}
else if (File.Exists(filename))
{
using (var str = new StreamWriter(filename))
{
str.WriteLine("msisdn: " + source);
str.WriteLine("shortcode : " + dest);
str.WriteLine("Message : " + messageIn);
str.WriteLine("Operator :" + operatorNew);
str.Flush();
}
}
Create返回FileStream,您需要StreamWriter。您必须使用其接受流的构造函数:
简化,使用FileStream创建或覆盖您的文件请参见下文,具体取决于您的需求您可能希望将文件更改为其他附加文件
public void WriteToFile(string name, string source, int dest, string messageIn, string operatorNew)
{
string directory = ResolveUrl("~/DesktopModules/SMSFunction/SMSText");
string filename = String.Format("{0:yyyy-MM-dd}__{1}", DateTime.Now,name);
string path = Path.Combine(directory, filename);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
using (StreamWriter str = new StreamWriter(fs))
{
str.WriteLine("msisdn: " + source);
str.WriteLine("shortcode : " + dest);
str.WriteLine("Message : " + messageIn);
str.WriteLine("Operator :" + operatorNew);
str.Flush();
}
}
}
您需要进行以下更改 1.将ResolveUrl替换为Server.MapPath 2.添加文件扩展名.txt,如下所示
string filename = String.Format("{0:yyyy-MM-dd}__{1}.txt", DateTime.Now,name);
3.检查文件是否存在时,请提供文件路径,而不是文件名
File.Exists(path);
var str = new StreamWriter(path));
4.在else if块下,此处还提供路径,而不是文件名
File.Exists(path);
var str = new StreamWriter(path));
把所有代码放在一起看起来像
string directory = Server.MapPath("~/DesktopModules/SMSFunction/SMSText");
string filename = String.Format("{0:yyyy-MM-dd}__{1}.txt", DateTime.Now, name);
string path = Path.Combine(directory, filename);
if (!File.Exists(path))
{
using (StreamWriter str = File.CreateText(path))
{
str.WriteLine("msisdn: " + source);
str.WriteLine("shortcode : " + dest);
str.WriteLine("Message : " + messageIn);
str.WriteLine("Operator :" + operatorNew);
str.Flush();
}
}
else if (File.Exists(path))
{
using (var str = new StreamWriter(path))
{
str.WriteLine("msisdn: " + source);
str.WriteLine("shortcode : " + dest);
str.WriteLine("Message : " + messageIn);
str.WriteLine("Operator :" + operatorNew);
str.Flush();
}
假设您的控制台应用程序项目名称为DataPrep。现在,您可以通过创建一个名为db.json的新文件来写入数据目录位置
string filePath = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\")) + @"Data\db.json";
if (!File.Exists(filePath))
{
using (StreamWriter _streamWriter = File.CreateText(filePath))
{
_streamWriter.Write(resultAll);
_streamWriter.Flush();
}
}
else if (File.Exists(filePath))
{
using (var _streamWriter = new StreamWriter(filePath))
{
_streamWriter.Write(resultAll);
_streamWriter.Flush();
}
}
我想知道文件是否已经存在,然后需要写入,或者如果不创建它并写入。是否尝试了文件。打开?或者不同的FileStream构造函数?我问过File.Open和FileStream。您更新的代码都没有。C初学者有问题,FileStream是指StreamWriter还是StreamReader?@NimanthaPrasad FileStream是指。您没有提供任何文件扩展名,请将.txt添加到filename@NimanthaPrasad我希望您理解为什么需要这些更改。我们可以从这里删除名称吗?我认为它是未使用的字符串filename=string.Format{0:yyyy-MM-dd}{1}.txt,DateTime.Now,name@NimanthaPrasad它的有用文件名是2012-08-20__xyz.txt,其中name=xyz。所以这完全取决于你的要求。