简体   繁体   English

C#无法写入文件(“被其他进程使用”)?

[英]C# can not write to file (“being used by another process”)?

I was hoping someone could explain to me why this is happening :) 我希望有人可以向我解释为什么会发生这种情况:)

When I use the code below it gives me the error "The process cannot access the file 'C:\\test.txt' because it is being used by another process." 当我使用下面的代码时,它给出了错误“进程无法访问文件'C:\\ test.txt',因为它正被另一个进程使用。”

I'm very new to C# so I'm not sure what is going on, thanks in advance! 我对C#很新,所以我不确定是怎么回事,提前谢谢!

String fileNameBefore = @"C:\\test.txt";

public void output(String hex)
{
    using (StreamWriter writer = new StreamWriter(fileNameBefore, true))
    {
        writer.Write(hex);
        writer.Close();
    }
}

Close the text file before you write to it. 在写入之前关闭文本文件。 (Not in code...physically close the text file) (不在代码中......物理关闭文本文件)

Under Windows Vista and Windows 7 the root of the system volume (usually C:) has a special protection: Programs have to run with full admin privileges to do anything other than creating or deleting non-system folders. 在Windows Vista和Windows 7下,系统卷的根目录(通常为C :)具有特殊保护:程序必须以完全管理员权限运行才能执行除创建或删除非系统文件夹之外的任何操作。 Programs aren't allowed to create files there. 程序不允许在那里创建文件。 Why it's saying the file is in use by another process? 为什么它说该文件正在被另一个进程使用? I'd expect another message, but I guess that's indeed the reason... Unless you've got some other program having that file opened and sufficient rights on the program whose code we see above. 我期待另一条消息,但我想这确实是原因......除非你有一些其他程序打开该文件并且对我们在上面看到的代码的程序有足够的权利。

Make sure the process is properly closed by checking in windows task manager and look for your process name, and if you find any end the processes manually by clicking the end process button and try it again. 通过检入Windows任务管理器并查找进程名称来确保正确关闭进程,如果通过单击结束进程按钮手动找到任何结束进程并再次尝试。

If that doesn't work try moving the file path to a writable area such as the desktop because as mario says the root has a protection. 如果这不起作用,请尝试将文件路径移动到可写区域,例如桌面,因为马里奥说根有保护。

also check that the file is not hidden or write protected 还检查文件是否未隐藏或写保护

Hope this helps regards Tom 希望这有助于汤姆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 写入C#中另一个进程(Windows服务)正在使用的文件 - Write to a file that is being used by another process (Windows Service) in C# 其他进程正在使用的C#文件 - C# File being used by another process 另一个进程正在使用C#文件 - C# file is being used by another process 文件正在由C#中的另一个进程使用 - File is being used by another process in c# C# “文件正在被另一个进程使用” - C# "File is being used by another process" 该进程无法访问文件'directory \\ file',因为它正被C#File Writer中的另一个进程使用 - The process can not access the file 'directory\file' because it is being used by another process in C# File Writer C#进程无法访问该文件,因为它正由另一个进程使用(文件对话框) - C# The process can't access the file because it is being used by another process (File Dialog) 进程无法访问另一个进程正在使用的文件C# - The process cannot access the file it is being used by another process c# 该进程无法访问文件,因为该文件正被另一个进程C#使用 - The process cannot acces the file as it is being used by another process C# C# File.Delete,另一个进程正在使用的文件 - C# File.Delete, file being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM