如何指定StreamWriter的路径位置 [英] How to specify the Streamwriter path location

查看:137
本文介绍了如何指定StreamWriter的路径位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个文本使用StreamWriter.But文件名应为当前日期的名字到文件。这里
是我的coding.Can有人告诉我如何指定文件创建路径



代码编辑:
在这里,我想创造一个.txt文件,但在这里文件未创建。

 公共无效将writeToFile(字符串名称,字符串源,INT DEST,串messageIn,串operatorNew)
{
字符串目录= RESOLVEURL(〜/ DesktopModules / SMSFunction / SMSText);
字符串文件名=的String.Format({0:YYYY-MM-DD} __ {1},DateTime.Now,名);
路径字符串= Path.Combine(目录下,文件名);

如果(!File.Exists(文件名))
{
$ B $使用(StreamWriter的海峡= File.CreateText(路径))
{$ B B $ b str.WriteLine(MSISDN:+源);
str.WriteLine(简码+ DEST);
str.WriteLine(消息:+ messageIn);
str.WriteLine(操作:+ operatorNew);
str.Flush();

}
}
,否则如果(File.Exists(文件名))
{

使用(VAR海峡=新的StreamWriter(文件名))

{
str.WriteLine(MSISDN:+源);
str.WriteLine(简码+ DEST);
str.WriteLine(消息:+ messageIn);
str.WriteLine(操作:+ operatorNew);
str.Flush();
}

}


解决方案

你需要做以下修改



1.Replace RESOLVEURL 服务器。在MapPath

 字符串目录=使用Server.Mappath(〜/ DesktopModules / SMSFunction / SMSText); 



2.添加文件扩展名 .TXT 如下图所示。

 字符串文件名=的String.Format({0:YYYY-MM-DD} __ {1} .TXT DateTime.Now,名); 



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

  File.Exists(路径); 



4.under的否则,如果块,这里还提供,而不是文件名的路径,

 无功海峡=新的StreamWriter(路径)); 



把所有在一起的代码看起来像,

 字符串目录=使用Server.Mappath(〜/ DesktopModules / SMSFunction / SMSText); 
字符串文件名=的String.Format({0:YYYY-MM-DD} __ {1}的.txt,DateTime.Now,名);
路径字符串= Path.Combine(目录下,文件名);

如果(!File.Exists(路径))
{使用(StreamWriter的海峡= File.CreateText(路径))
{
STR
。的WriteLine(MSISDN:+源);
str.WriteLine(简码+ DEST);
str.WriteLine(消息:+ messageIn);
str.WriteLine(操作:+ operatorNew);
str.Flush();
}
}
,否则如果(File.Exists(路径))
{
使用(VAR海峡=新的StreamWriter(路径))
$ { b $ b str.WriteLine(MSISDN:+源);
str.WriteLine(简码+ DEST);
str.WriteLine(消息:+ messageIn);
str.WriteLine(操作:+ operatorNew);
str.Flush();
}


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?

Code Edit : In here i wanted to create a .txt file but in here file not created.

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

   }

解决方案

you need to make following changes

1.Replace ResolveUrl with Server.MapPath

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

2.Add the file extension .txt as shown below

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

File.Exists(path);

4.under the else if block , here also provide the path , instead of filename

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

这篇关于如何指定StreamWriter的路径位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆