2012-08-17 62 views
-1

我想使用StreamWriter.But将文本写入文件,文件名应为当前日期名称。 这里是我的编码。可以有人告诉我如何指定文件创建路径?如何指定Streamwriter路径位置

代码编辑: 在这里我想创建一个.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(); 
     } 

    } 
+0

你没有提供任何文件扩展名,加上'.txt'为filename – Mourya 2012-08-17 11:41:05

回答

1

你需要做以下更改

1.Replace ResolveUrlServer.MapPath

string directory = Server.MapPath("~/DesktopModules/SMSFunction/SMSText"); 

2.添加如下图所示

string filename = String.Format("{0:yyyy-MM-dd}__{1}.txt", DateTime.Now,name); 

3.当您正在检查文件是否存在,或不提供文件的路径,而不是文件名

File.Exists(path); 

文件扩展名.txt 4.在else if区块下,这里也提供路径,而不是文件名

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(); 
    } 
+0

correct.thank you – 2012-08-20 06:11:10

+0

@NimanthaPrasad我希望你明白为什么需要这些改变。 – Mourya 2012-08-20 06:18:20

+0

我们可以从这里删除名字吗?我认为这是未使用的字符串文件名= String.Format(“{0:yyyy-MM-dd} __ {1} .txt”,DateTime.Now,name); – 2012-08-20 07:34:54

2

File.Create回报FileStream,你需要StreamWriter。你将不得不使用它的构造函数接受Stream

using (var str = new StreamWriter(File.CreateText(path))) 
+0

我想如果文件已经存在,则需要编写或如果没有创建并写入 – 2012-08-17 09:46:24

+1

你尝试过'File.Open'吗?或者不同的'FileStream'构造函数? – 2012-08-17 10:06:38

+0

代码编辑。但它不会创建.txt文件 – 2012-08-17 10:16:56

0

简化,使用的FileStream取决于你neeeds创建或覆盖您的文件(见下文),你可能想改变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(); 
     } 
    } 
}