如何在C#中创建文件夹 [英] How to create a folder in C#

查看:103
本文介绍了如何在C#中创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在我的程序exe旁边创建一个包含我的文本文件的文件夹?



我想在我的exe文件旁边创建一个文件夹将其txt文件保存在其中。

我创建一个新类,创建一个目录但是如何将其设置为我的Stream运算符的路径?换句话说,如何在变量中保存路径并将其传递给其他类和表单中的所有其他方法?



我尝试过的:



提前感谢您提供的信息!

How can I create a Folder that holds my text files,right next to my program exe?

I want to create a folder next to my exe file and save its txt files in it.
I create a new class,create a directory but how can I set it as a path for my Stream operators? In other words,how do I save the path in a variable and pass it to all other methods in the other classes and forms?

What I have tried:

Thank you for the information in advance!

推荐答案

MSDN文档几乎是始终有用:如何:创建文件或文件夹(C#编程指南) [ ^ ]
MSDN documentation is almost always helpful: How to: Create a File or Folder (C# Programming Guide)[^]


在WinForms中:



Application.StartUpPath会将路径(字符串)返回到文件夹所在的位置,通常,WinForm项目.exe位于。



Application.ExecutablePath将路径(字符串)返回到实际的可执行文件名。



以一种创建文件夹的方式获取文件夹路径的DirectoryInfo,如果它不是alr存在:
In WinForms:

Application.StartUpPath will return the path (string) to the folder where, usually, the WinForm project .exe is located.

Application.ExecutablePath will return the path (string) to the actual executable file name.

To get the DirectoryInfo of a folder path in a way that will create the folder if it does not already exist:
private DirectoryInfo AddDirectory(string targetFolder, string dirName)
{
    // screen for null
    if (String.IsNullOrEmpty(targetFolder) || String.IsNullOrEmpty(dirName))
    {
        throw new ArgumentException(string.Format("targetFolder {0}  and/or dirName {1} is empty)", targetFolder, dirName));
    }

    string folderPath = Path.Combine(targetFolder, dirName);

    // check for invalid characters
    if (folderPath.IndexOfAny(Path.GetInvalidPathChars()) != -1)
    {
        throw new InvalidDataException(String.Format("folderPath: {0} is invalid", folderPath));
    }

    if (Directory.Exists(folderPath))
    {
        return new DirectoryInfo(folderPath);
    }
    else
    {
        return Directory.CreateDirectory(folderPath);
    }
}

Envrionment.SpecialFolder枚举中提供了常用Windows文件夹的目录路径。 Environment Class还提供了一个静态方法'GetFolderPath,它将返回与特定'SpecialFolder枚举值相关的路径(字符串)。



创建一个WinForm项目,放一个TextBox,'主窗体上的textBox1,在Form Load Event中插入此代码:

The Directory paths to common Windows folders are available in the Envrionment.SpecialFolder enumeration. The Environment Class also offers a static method 'GetFolderPath that will return the path (string) associated with a specific 'SpecialFolder enum value.

Create a WinForm Project, put a TextBox, 'textBox1 on the main form, insert this code in the Form Load Event:

textBox1.Text = string.Format("StartUp Path:\r\n{0}\r\n\r\n", Application.StartupPath);
textBox1.AppendText(string.Format("Executable Path:\r\n{0}\r\n\r\n", Application.ExecutablePath));
            
foreach (Environment.SpecialFolder fldr in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
    var path = Environment.GetFolderPath(fldr).ToString();

    textBox1.AppendText(string.Format("SpecialFolder value: {0}\r\n", fldr.ToString()));

    if (string.IsNullOrWhiteSpace(path)) path = "path not relevant";

    textBox1.AppendText(string.Format("{0}\r\n\r\n", path));
}

当然,一旦你有了路径,你就可以使用Directory.Exists(SomeDirectoryPaath)来确定你是否已经创建了这个文件夹。

Of course, once you have the path, you can then use Directory.Exists(SomeDirectoryPaath) to determine if you've already created the folder.


要创建文件夹,只需使用:

To create a folder simply use this :
System.IO.Directory.CreateDirectory("directorypath");


这篇关于如何在C#中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆