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

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

问题描述

我想创建txt文件。



如果是第一次创建txt,你会得到number1。



如果先前创建了txt,则会得到数字+ 1. +1的出现次数加起来。(数字+ 1表示。但是我已经2次成为1号总是在继续。) />


我尝试过:



I want to create txt file.

If txt is created for the first time, you get number1.

If txt was previously created, you get the number + 1. The number of occurrences of +1 add up.(Number + 1 did. But 2 times I have been number 1 is always going on.)

What I have tried:

string path= @"C:\Example\Example.txt";
int number;
string foldername;
int count=0;

if(File.Exists(path))
{
   using (TextWriter tw=new StreamWriter(path))
{
    tw.Writeline();
    count++;
    number=count;
}
if(count !=0)
{
     number=number+1;
     foldername="T" +number;
}

}

else if (!File.Exists(path))
{
   File.Create(path).Dispose();
   using(TextWriter tw=new StreamWriter(path))
{
   tw.Writeline();
   count=1;
   foldername="T" + count;
}

}

推荐答案



你做的你的工作如此复杂。我给你写了这个部分,直到为你的文件名提供正确的索引。如果您需要更多帮助,请告诉我我将完成解决方案。

Hi,
You made your work so complicated. I wrote you the part till provides you the right index for your file name. If you needed more help then let me know I will complete the solution.
int count = 0;
string fileName = "Example.txt";
string path = @"C:\Example\";
if (File.Exists(Path.Combine(path, fileName)))
    count = Directory.EnumerateFiles(path).Count();
count++;
//Here you go the count now is ready to be your file index.
fileName = Path.Combine(path,


示例{count} .txt);





干杯,

AH



Cheers,
AH

你好像可以使用 do ... while 循环来浏览连续的文件,现在它看起来像你只尝试原始文件名和如果原始存在,则更改为raw1。



您可以将其合并到您的代码中以获取文件的最后编号版本。语法可能不是100%,因为我现在仅限于记事本

You looks like you could use a do...while loop to go through successive files, right now it looks like you try only the "raw" filename and change to raw1 if the original exists.

You could incorporate this into your code to get the last numbered version of the file. The syntax may not be 100% as I am limited to notepad at the moment
private int GetLastFileNumber(string FileFolder, string Filename, string FileExtension) {
  int FileCount  = 0;
  string FileCheck;
  string FileExists = True;
  do {
    if (FileCount == 0) { FileCheck = Filename + "." + FileExtension; }
    else { FileCheck = string.Format("{0}{1}.{2}", FileName, FileCount, FileExtension);
    FileExists = File.Exists(FileFolder+FileCheck)
    FileCount++
  } while(FileExists);
  return FileCount;
}


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

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