C# 在您的应用程序的位置保存文件
C# Save File at Location of Your App
我正在尝试保存到这样的文件中:
string path = AppDomain.CurrentDomain.BaseDirectory;
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
但是我得到 DirectoryNotFoundException 尽管目录存在。
方法在这里
public static void WriteToFile(string s)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
我不确定为什么会这样。谢谢
您需要指定文件名,仅目录不足以创建文件:
public static void WriteToFile(string s, string fileName)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
用法:
WriteToFile("Some Text To Write", "SampleFileName.txt");
这里是文档:
public FileStream(string path, FileMode mode, FileAccess access);
Parameters:
path: A relative or absolute path for the file that the current FileStream object will encapsulate.
你的'路径是一个目录,但是System.IO.FileStream.FileStream(string path, FileMode mode, FileAccess access)需要一个文件路径,所以我修改你的代码如下:
public static void WriteToFile(string s)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "a.txt";
var fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
var sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine(path);
Console.ReadKey();
}
输出:
d:\users\zwguo\documents\visual工作室2013\Projects\ConsoleApplication15\Consol
eApplication15\bin\Debug\a.txt
我正在尝试保存到这样的文件中:
string path = AppDomain.CurrentDomain.BaseDirectory;
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
但是我得到 DirectoryNotFoundException 尽管目录存在。
方法在这里
public static void WriteToFile(string s)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
我不确定为什么会这样。谢谢
您需要指定文件名,仅目录不足以创建文件:
public static void WriteToFile(string s, string fileName)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
}
用法:
WriteToFile("Some Text To Write", "SampleFileName.txt");
这里是文档:
public FileStream(string path, FileMode mode, FileAccess access);
Parameters: path: A relative or absolute path for the file that the current FileStream object will encapsulate.
你的'路径是一个目录,但是System.IO.FileStream.FileStream(string path, FileMode mode, FileAccess access)需要一个文件路径,所以我修改你的代码如下:
public static void WriteToFile(string s)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "a.txt";
var fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);
var sw = new System.IO.StreamWriter(fs);
sw.WriteLine(s);
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine(path);
Console.ReadKey();
}
输出: d:\users\zwguo\documents\visual工作室2013\Projects\ConsoleApplication15\Consol eApplication15\bin\Debug\a.txt