2010-11-22 14 views
-1

名前を付けて保存ダイアログを表示せずにファイルを保存することはできますか?saveasダイアログでファイルを保存しますか?

+2

ありますか? – Bolu

+3

実際には、「名前を付けて保存」ダイアログは、ファイルを保存(作成/書き込み/上書き/追加)する行為とほとんど関係がありません。 – Ani

答えて

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

はい、ファイル名が必要です。ファイルダイアログは、ファイルを書き込む良いファイル名を決定するためだけです。

関連する問題