C# 如何指定 Streamwriter 路径位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12003110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify the Streamwriter path location
提问by Nimantha Prasad
I wanted to write a text to file using StreamWriter.But Filename should be current date name. here is my coding.Can somebody tell me how to specify the file creation path?
我想使用 StreamWriter 将文本写入文件。但文件名应该是当前日期名称。这是我的编码。有人能告诉我如何指定文件创建路径吗?
Code Edit : In here i wanted to create a .txt file but in here file not created.
代码编辑:在这里我想创建一个 .txt 文件,但在这里没有创建文件。
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();
}
}
采纳答案by Mourya
you need to make following changes
您需要进行以下更改
1.Replace ResolveUrlwith Server.MapPath
1.ResolveUrl替换为Server.MapPath
string directory = Server.MapPath("~/DesktopModules/SMSFunction/SMSText");
2.Add the file extension .txtas shown below
2.添加文件扩展名.txt,如下图
string filename = String.Format("{0:yyyy-MM-dd}__{1}.txt", DateTime.Now,name);
3.when you are checking whether file exists or not provide the path of the file , instead of filename
3.当你检查文件是否存在时,提供文件的路径,而不是文件名
File.Exists(path);
4.under the else ifblock , here also provide the path , instead of filename
4.在else if块下,这里也提供路径,而不是文件名
var str = new StreamWriter(path));
putting all together the code looks like,
把所有的代码放在一起,看起来像,
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();
}
回答by Serg Rogovtsev
File.Createreturns FileStream, and you need StreamWriter. You'll have to use its constructor that accepts Stream:
File.Create返回FileStream,您需要StreamWriter. 您必须使用其接受的构造函数Stream:
using (var str = new StreamWriter(File.CreateText(path)))
回答by Tommy Grovnes
Simplify, use FileStream to create or overwrite your file (see below) depending on your neeeds you might want to change the FileModeto be something else (Append ?)
简化,根据您的需要,使用 FileStream 创建或覆盖您的文件(见下文),您可能希望将FileMode更改为其他内容(附加?)
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();
}
}
}
回答by R M Shahidul Islam Shahed
Let's say your console app project name is DataPrep. Now you can write in Data directory location by creating a new file namely as db.json
假设您的控制台应用程序项目名称是 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();
}
}

