StreamWriter删除C#控制台应用程序中的路径问题 [英] StreamWriter delete path problem in C# console application
问题描述
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.IO;
命名空间ConsoleApplication1
{
class Program
{
static void Main(string [] args)
{
随机rnd = new Random();
int Random = rnd.Next(1,100);
string path = @" C:\ test\" + Random +" .txt";
Console.WriteLine(" [1] Creat pin");
Console.WriteLine ([2]删除);
string pick = Console.ReadLine();
开关(选择)
{
案例1:
使用(StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine(随机) );
}
休息;
案例2:
string path1 = @C:\\\\+ path +。txt";
Console.ReadLine();
if(File.Exists(path)== false)
{
File.Delete(路径);
}
else
{
Console.WriteLine(No file" );
}
休息;
}
}
}
无法删除文件路径..?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int Random = rnd.Next(1, 100);
string path = @"C:\test\" + Random + ".txt";
Console.WriteLine("[1]Creat pin");
Console.WriteLine("[2]Delete");
string pick = Console.ReadLine();
switch (pick)
{
case "1":
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine(Random);
}
break;
case "2":
string path1 = @"C:\test\"+ path +".txt";
Console.ReadLine();
if (File.Exists(path)==false)
{
File.Delete(path);
}
else
{
Console.WriteLine("No file"");
}
break;
}
}
}
cant delete file path..?
推荐答案
当然不是。由于您的程序只运行一个输入,因此每次运行时,Random都将是一个不同的值。因此,一旦创建,程序就不知道它最后一次做了什么,并为路径生成了不同的值
。
Of course not. Since your program only runs for one input, Random is going to be a different value every time you run it. So, once created, the program doesn't know what it did the last time and generates a different value for path
.
这篇关于StreamWriter删除C#控制台应用程序中的路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!