C# 是否使用“另存为”对话框保存文件?
我能在没有“另存为”对话框的情况下保存文件吗?你的问题非常含糊,但我还是大胆地尝试了一下: 是的,只需使用中的类即可。举个例子来说,在;引用它:C# 是否使用“另存为”对话框保存文件?,c#,C#,我能在没有“另存为”对话框的情况下保存文件吗?你的问题非常含糊,但我还是大胆地尝试了一下: 是的,只需使用中的类即可。举个例子来说,在;引用它: using System; using System.IO; using System.Text; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if
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);
}
}
使用系统;
使用System.IO;
使用系统文本;
课堂测试
{
公共静态void Main()
{
字符串路径=@“c:\temp\MyTest.txt”;
//删除文件(如果存在)。
if(File.Exists(path))
{
删除(路径);
}
//创建文件。
使用(FileStream fs=File.Create(path))
{
AddText(fs,“这是一些文本”);
AddText(fs,“这是更多的文本”);
AddText(fs,“\r\n这是一个新行”);
AddText(fs,“\r\n\r\n以下是字符的子集:\r\n”);
对于(int i=1;i<120;i++)
{
AddText(fs,Convert.ToChar(i.ToString());
}
}
//打开流并将其读回。
使用(FileStream fs=File.OpenRead(path))
{
字节[]b=新字节[1024];
UTF8Encoding temp=新的UTF8Encoding(真);
而(fs.Read(b,0,b.Length)>0)
{
控制台写入线(临时GetString(b));
}
}
}
私有静态void AddText(FileStream fs,字符串值)
{
byte[]info=新的UTF8Encoding(真)。GetBytes(值);
fs.Write(信息,0,信息长度);
}
}
@All:同样,这是文档中的引用,而不是原始代码。当然。您可以使用StreamWriter类:
FileInfo t = new FileInfo("f.txt");
StreamWriter Tex =t.CreateText();
Tex.WriteLine("Hello");
Tex.close();
是的,请检查该类。是的,但必须有文件名。“文件”对话框仅用于确定写入文件的正确文件名。如果您知道要保存文件的路径,请确定。
您可以使用、like或的方法 实际上,“另存为”对话框与保存(创建/写入/重写/追加)文件的行为几乎没有关系。