启动 MS DOS 命令“attrib”作为进程 (C#)

发布于 2024-11-04 11:49:23 字数 438 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2024-11-11 11:49:23

您要求什么:

string hideOrShow = (hide) ? "+" : "-";
Process.Start("cmd /c attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");

您应该做什么:

File.SetAttributes(path, FileAttributes.Hidden);

What you asked for:

string hideOrShow = (hide) ? "+" : "-";
Process.Start("cmd /c attrib " + hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");

What you should do instead:

File.SetAttributes(path, FileAttributes.Hidden);
你的呼吸 2024-11-11 11:49:23

Process.Start() 的第一个参数必须是可执行文件或文档的名称。您需要传递两个参数,如下所示:

Process.Start("attrib.exe", hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");

另外,虽然 attrib.exe 在直接调用时会起作用,但大多数人会将这种 DOS 风格的命令传递给命令解释器(这也适用于内置命令) , ETC。)

Process.Start("cmd.exe", "/c attrib " + restOfTheArguments);

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:

Process.Start("attrib.exe", hideOrShow + "h " + hideOrShow + "s \"" + path + "\" /S /D");

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.)

Process.Start("cmd.exe", "/c attrib " + restOfTheArguments);
橘和柠 2024-11-11 11:49:23

C# 使这变得非常简单 - 这个想法是你获取文件的当前属性 (File.GetAttributes()),然后在调用 File.SetAttributes() 之前添加隐藏属性

,检查下面的内容,它将使 c:\blah隐藏

static void Main(string[] args)
{
    FileAttributes oldAttributes = File.GetAttributes(@"c:\blah");
    File.SetAttributes(@"c:\blah", oldAttributes | FileAttributes.Hidden);
}

要删除隐藏属性,您需要删除隐藏属性

static void Main(string[] args)
{
    FileAttributes newAttributes = File.GetAttributes(@"c:\blah");
    newAttributes = newAttributes & (~FileAttributes.Hidden);

    File.SetAttributes(@"c:\blah", newAttributes);
}

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

static void Main(string[] args)
{
    FileAttributes oldAttributes = File.GetAttributes(@"c:\blah");
    File.SetAttributes(@"c:\blah", oldAttributes | FileAttributes.Hidden);
}

to remove the hidden attribute you need to remove the hidden attribute

static void Main(string[] args)
{
    FileAttributes newAttributes = File.GetAttributes(@"c:\blah");
    newAttributes = newAttributes & (~FileAttributes.Hidden);

    File.SetAttributes(@"c:\blah", newAttributes);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文