可执行文件的目录路径
本文关键字:路径 可执行文件 | 更新日期: 2023-09-27 18:05:57
你好,我正在尝试在我的第一个windows窗体应用程序可执行文件中创建带有文本文档的目录文件,但这里有问题:
我想让它与其他本地用户计算机的exe文件一起使用:
string dir = @"C:'Users'Public'AppData'Roaming'AppFolder'document.txt";
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(Path.GetDirectoryName(dir));
var stream = File.CreateText(dir);
}
但是我得到了这个:
未处理的System.IO类型异常。发生IOException'mscorlib.dll
附加信息:进程无法访问该文件"C: ' '公共' AppData '漫游用户' AppFolder ' doc.txt"因为它正在被其他进程使用。
我猜你需要发出命令,让流在其他任何地方都可以访问
stream.Flush();
stream.Close();
尝试用using
语句来拥抱CreateText
。用完之后就关闭了。File.CreateText
将创建该文件,但它将保持打开状态,直到它将被关闭。试图打开它两次将导致IOException
。
此片段是https://msdn.microsoft.com/de-de/library/system.io.file.createtext(v=vs.110).aspx
示例的一部分 string path = @"c:'temp'MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
要访问/创建AppData漫游文件夹中的文件/目录,您必须执行以下操作
// The folder for the roaming current user
string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
// Combine the base folder with your specific folder....
string specificFolder = Path.Combine(folder, "YourSpecificFolder");
// Check if folder exists and if not, create it
if(!Directory.Exists(specificFolder))
Directory.CreateDirectory(specificFolder);