如何在目录中动态创建 [英] How create in directory dynamically

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

问题描述

你好,
我想在该区域中创建一个文件夹,但是在myproject中创建了文件夹
您可以指导我还是发送任何摘录

Hello,
i want to create a folder in this area but folder is created in myproject
can u guide me or send any snippets

paths = @"C:Documents and Settings\mindtech\My Documents\Visual Studio 2010\WebSites\myproject\consult\man\";

       }


      if (!System.IO.Directory.Exists(paths))
       {
           string news = TextBox1.Text.Trim();
          // string newPath = System.IO.Path.Combine(paths,news);
          paths +=TextBox1.Text;
           System.IO.Directory.CreateDirectory(paths);
           //lblError.Text = "Created Successfully";
       }

推荐答案

有用的链接:
http://msdn.microsoft.com/en-us/library/as2f1fez.aspx [ ^ ]
http://www.dotnetspider.com/resources/28277-Create-Folder-Subfolder.aspx [ ^ ]
http://blog.logiclabz.com/c/copy- directory-in-net-c-includes-sub-folders.aspx [
Usuful links:
http://msdn.microsoft.com/en-us/library/as2f1fez.aspx[^]
http://www.dotnetspider.com/resources/28277-Create-Folder-Subfolder.aspx[^]
http://blog.logiclabz.com/c/copy-directory-in-net-c-including-sub-folders.aspx[^]


男孩.我从哪说起呢?首先,您已经对此进行了标记,因此这里的含义是您试图在Web服务器上创建目录.正确受保护的Web应用程序可以在非常严格的安全沙箱中运行,因此您不应在Web应用程序的文件夹结构之外编写.
Hoo boy. Where do I start? First of all, you have tagged this so the implication here is that you are attempting to create the directory on a web server. Properly secured web applications run in a very tight security sandbox, so you shouldn''t write outside the folder structure of the web application.
paths = @"C:Documents and Settings\mindtech\My Documents\Visual Studio 2010\WebSites\myproject\consult\man\";

此路径是错误的.您在c:之后错过了\.

您创建子目录的逻辑有些怀疑.首先,您测试以查看在路径中指定的目录是否存在.如果没有,则向该名称添加另一个文件夹并尝试创建该文件夹.出现顶级目录时会发生什么?您创建子目录的代码不会执行.请尝试以下操作:

This path is wrong. You missed the \ after c:.

Your logic for the creation of the sub-directory is a bit suspect. First of all, you test to see if the directory you specified in paths exists. If it doesn''t, you add another folder to this name and attempt to create that. What happens when the top level directory is present? Your code to create the sub directory doesn''t get executed. Try this instead:

private void CreateDirectory(string path, string subPath)
{
  string folder = Path.Combine(path, subPath);
  Directory.CreateDirectory(folder);
}

需要注意的重要一点是,如果目录已经存在,则此方法将不执行任何操作,因为Directory.CreateDirectory检查路径是否已经存在.

The important thing to note there is that if the directory already exists, this method does nothing as Directory.CreateDirectory checks to see if the path is already present.


public class CreateFileOrFolder
{
    static void Main()
    {
        // Specify a "currently active folder"
        string activeDir = @"C:Documents and Settings\mindtech\My Documents\Visual Studio 2010\WebSites\myproject\consult\man\";

        //Create a new subfolder under the current active folder
        string newPath = System.IO.Path.Combine(activeDir, "mySubDir");

        // Create the subfolder
        System.IO.Directory.CreateDirectory(newPath);

        // Create a new file name. This example generates
        // a random string.
        string newFileName = TextBox1.Text.Trim();

        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(newPath, newFileName);

        // Create the file and write to it.
        // DANGER: System.IO.File.Create will overwrite the file
        // if it already exists. This can occur even with
        // random file names.
        if (!System.IO.File.Exists(newPath))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
                for (byte i = 0; i < 100; i++)
                {
                    fs.WriteByte(i);
                }
            }
        }

        // Read data back from the file to prove
        // that the previous code worked.
        try
        {
            byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
            foreach (byte b in readBuffer)
            {
                Console.WriteLine(b);
            }
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }

        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}






最好的:-)






All the Best:-)


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

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