启动 MS DOS 命令“attrib”作为进程 (C#)
我正在尝试使用 MSDOS“attrib”命令用 C# 隐藏文件夹。
现在,我可以通过在批处理文件中编写“attrib”命令+参数,使用 Process.Start() 运行该文件,然后删除它来做到这一点。我想知道,我可以直接从 C# 做到这一点吗?
这是我到目前为止所尝试的...(下面的代码不起作用)
public static void hideFolder(bool hide, string path)
{
string hideOrShow = (hide) ? "+" : "-";
Process.Start("attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");
}
任何帮助将不胜感激! 谢谢!
I'm trying to hide a folder with C# using the MSDOS "attrib" command.
For now i'm able to do that by writing the "attrib" command + arguments in a batch file, running that file using Process.Start()
, and then deleting it. I was wondering, can I do that directly from C#?
Here is what i've tryed so far... (the code below doesen't work)
public static void hideFolder(bool hide, string path)
{
string hideOrShow = (hide) ? "+" : "-";
Process.Start("attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");
}
Any help would be appriciated!
Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您要求什么:
您应该做什么:
What you asked for:
What you should do instead:
Process.Start() 的第一个参数必须是可执行文件或文档的名称。您需要传递两个参数,如下所示:
另外,虽然 attrib.exe 在直接调用时会起作用,但大多数人会将这种 DOS 风格的命令传递给命令解释器(这也适用于内置命令) , ETC。)
The first parameter to Process.Start() needs to be the name of an executable file or document. You'll need to pass in two parameters, like this:
Also, while attrib.exe will work when called directly, most people will pass this kind of DOS-style command to the command interpreter (which will also work for built-in commands, etc.)
C# 使这变得非常简单 - 这个想法是你获取文件的当前属性 (File.GetAttributes()),然后在调用 File.SetAttributes() 之前添加隐藏属性
,检查下面的内容,它将使 c:\blah隐藏
要删除隐藏属性,您需要删除隐藏属性
C# makes this really easy - the idea is you get the files current attributes (File.GetAttributes()), then you add in the Hidden attribute before calling File.SetAttributes()
check the below out, it'll make c:\blah hidden
to remove the hidden attribute you need to remove the hidden attribute
有什么错误?为什么不使用 http://msdn.microsoft.com/ en-us/library/system.io.filesysteminfo.attributes.aspx ?
What's the error? Why not use http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.attributes.aspx ?