C#将文件保存在应用程序的位置
我正在尝试保存到如下文件:C#将文件保存在应用程序的位置,c#,filestream,streamwriter,C#,Filestream,Streamwriter,我正在尝试保存到如下文件: string path = AppDomain.CurrentDomain.BaseDirectory; fs = new System.IO.FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write); 但我得到的是DirectoryNotFoundException,尽管目录存在 这里是方法 public static void WriteToFile(strin
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);
参数:
路径:当前FileStream对象将封装的文件的相对或绝对路径
您需要指定文件名,仅指定目录不足以创建文件:
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);
参数:
路径:当前FileStream对象将封装的文件的相对或绝对路径
您需要指定文件名,仅指定目录不足以创建文件:
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);
参数:
路径:当前FileStream对象将封装的文件的相对或绝对路径
您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess访问)需要一个文件路径,因此我修改您的代码如下:
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 studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess)需要一个文件路径,因此我修改您的代码如下:
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 studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt您的路径是一个目录,但System.IO.FileStream.FileStream(字符串路径、FileMode模式、FileAccess)需要一个文件路径,因此我修改您的代码如下:
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 studio 2013\Projects\ConsoleApplication15\Console
eApplication15\bin\Debug\a.txt很有可能
path
指向目录(大约100%)。您能否澄清一下,为什么希望目录路径的文件创建成功?很有可能path
指向目录(大约100%)。您能否澄清一下,为什么希望目录路径的文件创建成功?很有可能path
指向目录(大约100%)。请您澄清为什么希望目录路径的文件创建成功?您应该始终将路径部分与路径合并。合并您应该始终将路径部分与路径合并。合并您应该始终将路径部分与路径合并。合并