C# 为什么我的程序循环?
在过去几天的实习期间,我一直在编写一个用C语言编写的简单控制台程序(我是一名编程新手,所以这花了我几天的时间才同意) 整个代码可在此处找到:C# 为什么我的程序循环?,c#,loops,attributes,readonly,C#,Loops,Attributes,Readonly,在过去几天的实习期间,我一直在编写一个用C语言编写的简单控制台程序(我是一名编程新手,所以这花了我几天的时间才同意) 整个代码可在此处找到: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace ConsoleApplication5 { class Pro
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication5
{
class Program
{
static int maxcheck = 1000;
static int stopsearch = 0;
public static void ProcessDirectory(string targetDirectory, List<string> foundFiles, List<string> errorFiles)
{
try
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
{
if (foundFiles.Count() >= maxcheck)
{
ConsoleKeyInfo answer;
Console.Clear();
Console.SetCursorPosition(2, 2);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("{0} files has been searched.",maxcheck);
Console.Write(" Do you wish to continue (Y/N): ");
Console.ForegroundColor = ConsoleColor.Green;
answer = Console.ReadKey();
Console.Clear();
if(answer.Key == ConsoleKey.Y)
{
maxcheck = maxcheck + 1000;
}
if(answer.Key ==ConsoleKey.N)
{
stopsearch = stopsearch + 1;
}
}
else
{
ProcessFile(fileName, foundFiles, errorFiles);
}
}
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory, foundFiles, errorFiles);
}
catch(Exception)
{
errorFiles.Add(targetDirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string fileName, List<string> changedFiles, List<string> errorfiles)
{
//Console.WriteLine("Processed file '{0}'.", path);
try
{
System.IO.FileAttributes attr = System.IO.File.GetAttributes(fileName);
if ((attr & System.IO.FileAttributes.ReadOnly) == 0)
{
attr = attr | System.IO.FileAttributes.ReadOnly;
System.IO.File.SetAttributes(fileName, attr);
changedFiles.Add(fileName);
}
}
catch (UnauthorizedAccessException)
{
errorfiles.Add(fileName);
}
catch (InvalidOperationException)
{
errorfiles.Add(fileName);
}
catch (Exception)
{
errorfiles.Add(fileName);
}
}
static void SetAllFilesAsReadOnly(string rootPath)
{
Console.ForegroundColor = ConsoleColor.White;
List<string> errorfiles = new List<string>();
List<string> changedfiles = new List<string>();
if (stopsearch < 1)
{
ProcessDirectory(rootPath, changedfiles, errorfiles);
}
if (changedfiles.Count() > 0)
{
Console.SetCursorPosition(2, 2);
Console.Write("Press any key to see the files that was changed");
Console.ForegroundColor = ConsoleColor.Green;
Console.ReadKey();
Console.Clear();
Console.SetCursorPosition(2, 2);
foreach (string file in changedfiles)
{
Console.WriteLine(" " + file);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
Console.WriteLine(" Write-protection was set on {0} files.", changedfiles.Count());
}
else
{
Console.SetCursorPosition(2, 2);
Console.Write("Write-protection was not changed on any files.");
}
Console.ForegroundColor = ConsoleColor.Black;
Console.ReadKey();
if (errorfiles.Count() > 0)
{
Console.WriteLine("");
Console.WriteLine(" These are the files that was NOT changed:");
foreach (string file in errorfiles)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(" " + file);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("");
Console.WriteLine(" Write-protection could not be set on {0} files.", errorfiles.Count());
Console.ReadKey();
}
}
static void Main(string[] args)
{
string pathstring;
if ((args.Count() == 0) || (args[0] == ""))
{
Console.SetCursorPosition(2, 2);
Console.Write("Please enter the path of the directory you wish to check: ");
Console.ForegroundColor = ConsoleColor.Green;
pathstring = Console.ReadLine();
}
else
{
pathstring = args[0];
}
Console.Clear();
SetAllFilesAsReadOnly(pathstring);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
命名空间控制台应用程序5
{
班级计划
{
静态int maxcheck=1000;
静态int-stopsearch=0;
公共静态void ProcessDirectory(字符串targetDirectory、列表foundFiles、列表errorFiles)
{
尝试
{
//处理在目录中找到的文件列表。
字符串[]fileEntries=Directory.GetFiles(targetDirectory);
foreach(文件项中的字符串文件名)
{
if(foundFiles.Count()>=maxcheck)
{
ConsoleKeyInfo答案;
Console.Clear();
控制台。设置光标位置(2,2);
Console.ForegroundColor=ConsoleColor.White;
WriteLine(“{0}个文件已被搜索。”,maxcheck);
控制台。写入(“您是否希望继续(Y/N):”;
Console.ForegroundColor=ConsoleColor.Green;
answer=Console.ReadKey();
Console.Clear();
if(answer.Key==ConsoleKey.Y)
{
maxcheck=maxcheck+1000;
}
if(answer.Key==ConsoleKey.N)
{
stopsearch=stopsearch+1;
}
}
其他的
{
ProcessFile(文件名、foundFiles、errorFiles);
}
}
//递归到此目录的子目录中。
string[]subdirectory entries=Directory.GetDirectories(targetDirectory);
foreach(子目录入口中的字符串子目录)
ProcessDirectory(子目录、foundFiles、errorFiles);
}
捕获(例外)
{
errorFiles.Add(targetDirectory);
}
//在此处插入处理找到的文件的逻辑。
公共静态void进程文件(字符串文件名、列表更改文件、列表错误文件)
{
//WriteLine(“已处理文件“{0}.”,路径);
尝试
{
System.IO.FileAttributes attr=System.IO.File.GetAttributes(文件名);
if((attr&System.IO.FileAttributes.ReadOnly)=0)
{
attr=attr | System.IO.FileAttributes.ReadOnly;
System.IO.File.SetAttributes(文件名,attr);
添加(文件名);
}
}
捕获(未经授权的访问例外)
{
errorfiles.Add(文件名);
}
捕获(无效操作异常)
{
errorfiles.Add(文件名);
}
捕获(例外)
{
errorfiles.Add(文件名);
}
}
静态void SetAllFilesAsReadOnly(字符串根路径)
{
Console.ForegroundColor=ConsoleColor.White;
List errorfiles=新列表();
列表更改文件=新列表();
如果(停止搜索<1)
{
ProcessDirectory(根路径、变更文件、错误文件);
}
如果(changedfiles.Count()>0)
{
控制台。设置光标位置(2,2);
编写(“按任意键查看已更改的文件”);
Console.ForegroundColor=ConsoleColor.Green;
Console.ReadKey();
Console.Clear();
控制台。设置光标位置(2,2);
foreach(changedfiles中的字符串文件)
{
Console.WriteLine(“+文件);
}
Console.ForegroundColor=ConsoleColor.White;
控制台。写线(“”);
WriteLine(“在{0}个文件上设置了写保护。”,changedfiles.Count());
}
其他的
{
控制台。设置光标位置(2,2);
Write(“任何文件上的写保护都没有更改。”);
}
Console.ForegroundColor=ConsoleColor.Black;
Console.ReadKey();
如果(errorfiles.Count()>0)
{
控制台。写线(“”);
WriteLine(“这些是未更改的文件:”);
foreach(errorfiles中的字符串文件)
{
Console.ForegroundColor=ConsoleColor.Red;
Console.WriteLine(“+文件);
}
Console.ForegroundColor=ConsoleColor.Green;
控制台。写线(“”);
WriteLine(“无法对{0}个文件设置写保护。”,errorfiles.Count());
Console.ReadKey();
}
}
静态void Main(字符串[]参数)
{
字符串路径字符串;
if((args.Count()==0)| |(args[0]==“”)
{
控制台。设置光标位置(2,2);
编写(“请输入要检查的目录的路径:”);
Console.ForegroundColor=ConsoleColor.Green;
pathstring=Console.ReadLine();
}
其他的
{
pathstring=args[0];
}
Console.Clear();
setAllFileAsReadOnly(路径字符串);
}
}
}
基本上,它所做的是用户在控制台窗口中写入一个目录路径,程序扫描其中的所有文件以获取只读属性
T
if(answer.Key == ConsoleKey.Y)
{
maxcheck = maxcheck + 1000;
}
if(answer.Key ==ConsoleKey.N)
{
return;
}
public static bool ProcessDirectory(string targetDirectory, List<string> foundFiles, List<string> errorFiles)
{
try
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
if (foundFiles.Count() >= maxcheck)
{
ConsoleKeyInfo answer;
Console.Clear();
Console.SetCursorPosition(2, 2);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("{0} files has been searched.", maxcheck);
Console.Write(" Do you wish to continue (Y/N): ");
Console.ForegroundColor = ConsoleColor.Green;
answer = Console.ReadKey();
Console.Clear();
if (answer.Key == ConsoleKey.Y)
{
maxcheck = maxcheck + 1000;
}
if (answer.Key == ConsoleKey.N)
{
return false;
}
}
else
{
ProcessFile(fileName, foundFiles, errorFiles);
}
}
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
if (!ProcessDirectory(subdirectory, foundFiles, errorFiles))
return false;
return true;
}
catch (Exception)
{
errorFiles.Add(targetDirectory);
return false; // or true if you want to continue in the face of exceptions.
}
}