2010-11-22 151 views
-1

我可以保存一个文件而不另存为对话框吗?用saveas对话框保存文件?

+2

有或没​​有? – Bolu 2010-11-22 12:56:13

+3

实际上,“另存为”对话框与*保存*(创建/写入/覆盖/附加)文件的行为很少有关。 – Ani 2010-11-22 12:57:41

回答

1

你提的问题是非常模糊的,但服用野生刺吧:

是的,只是使用的类在System.IO namespace。例如,在FileStream class documentation中有一个例子;引用它:

using System; 
using System.IO; 
using System.Text; 

class Test 
{ 

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

     // Delete the file if it exists. 
     if (File.Exists(path)) 
     { 
      File.Delete(path); 
     } 

     //Create the file. 
     using (FileStream fs = File.Create(path)) 
     { 
      AddText(fs, "This is some text"); 
      AddText(fs, "This is some more text,"); 
      AddText(fs, "\r\nand this is on a new line"); 
      AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); 

      for (int i=1;i < 120;i++) 
      { 
       AddText(fs, Convert.ToChar(i).ToString()); 

      } 
     } 

     //Open the stream and read it back. 
     using (FileStream fs = File.OpenRead(path)) 
     { 
      byte[] b = new byte[1024]; 
      UTF8Encoding temp = new UTF8Encoding(true); 
      while (fs.Read(b,0,b.Length) > 0) 
      { 
       Console.WriteLine(temp.GetString(b)); 
      } 
     } 
    } 

    private static void AddText(FileStream fs, string value) 
    { 
     byte[] info = new UTF8Encoding(true).GetBytes(value); 
     fs.Write(info, 0, info.Length); 
    } 
} 

@All:同样,这是从文档引用,而不是原始代码。

1

当然。您可以使用StreamWriter类:

FileInfo t = new FileInfo("f.txt"); 
StreamWriter Tex =t.CreateText(); 
Tex.WriteLine("Hello"); 
Tex.close(); 
1

是的,请查看File类。

3

是的,但你必须有一个文件名。文件对话框仅用于确定写入文件的好文件名。