在C#的同一文件夹中创建文件和文件夹 [英] create file and folder in the same folder in c#
本文介绍了在C#的同一文件夹中创建文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#中的另一个文件夹内创建文件和文件夹.
注意:file.txt
How to create file and folder inside another folder in C#.
Note: file.txt
推荐答案
阅读了发布的解决方案和您的评论后,我想要的"太多,而这就是我所做的"而这就是我被困住的地方",即您自己并没有付出任何努力.
我建议您阅读System.io
命名空间中的各个类的功能以及您如何使用它们来完成任务的方式获得可用的文档.
System.IO命名空间 [
Having read the solutions posted and your comments back, there is too much "I want" and not enough "This is what I have done and this is where I have got stuck" i.e. you are not making any effort yourself.
I suggest you go and read the documentation available on what the various classes in theSystem.io
namespace do, and how you can use them to achieve your task.
System.IO Namespace[^]
看看:
http://msdn.microsoft.com/en-us/library/as2f1fez.aspx [ ^ ]
http://www.dotnetperls.com/directory-createdirectory [
Have a look:
http://msdn.microsoft.com/en-us/library/as2f1fez.aspx[^]
http://www.dotnetperls.com/directory-createdirectory[^]
Hope they give you a point to start
朋友,这是送达您盘子的菜:
Freind, This is the dish served to your plate:
public class CreateFileOrFolder
{
static void Main()
{
// Specify a "currently active folder"
string activeDir = @"c:\testdir2";
//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 = System.IO.Path.GetRandomFileName();
// 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();
}
}
谢谢,
Ambesha
Thanks,
Ambesha
这篇关于在C#的同一文件夹中创建文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文