第一篇文章,零编程历史,但我自己学习了google和Udemy类。
我写了一个程序,它将在我们的服务器上创建一堆项目目录。我需要检查路径中当前目录的前6个字符,以查看是否已经使用了项目编号(该目录长于6位数字,但前6个是项目编号,然后是项目名称)无法在任何网站(包括该网站)上找到任何信息。
enter code here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
/* Created on 1/7/2019 by James L. Miller
* updated on 2/13/2019 to get the year automagically
**/
namespace MMEFolders
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// this sets the string for location and clears it
string project = null;
// this sets the current year
string year = DateTime.Now.Year.ToString();
// this sets the string for Server (and adds current Year variable)
if (radioButton1.Checked)
{
project = @"\\mmejaxdc1\projects\" + year + "\\";
}
else if (radioButton2.Checked)
{
project = @"\\mmeatldc\projects\" + year + "\\";
}
else if (radioButton3.Checked)
{
project = @"\\mmecltdc\projects\" + year + "\\";
}
else if (radioButton4.Checked)
{
project = @"\\mmecltdc\raleigh\projects\" + year + "\\";
}
else
{
MessageBox.Show("No Location was Selected!");
return;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Code to create the folders here
// this get the full project number
var ProjectNo = textBox1.Text;
// this addes the path to the server along with the project#
var PathString = project + ProjectNo;
////////////////////////////////////////////////////////////////////////////////////////////////
// this cuts off everything after the project number
var ProjNo = ProjectNo.Substring(0, 6);
////////////////////////////////////////////////////////////////////////////////////////////////
//this checks the server code VS the selected server
////////////////////////////////////////////////////////////////////////////////////////////////
//this checks the project number to see if it exists
/////////////////////////////////////////////////////////////////////////////////////////////////
// this creates the DESIGN folder
string subfolder1 = Path.Combine(PathString, (ProjNo + "_Design"));
Directory.CreateDirectory(subfolder1);
// this copies the QAQC doc to the DESIGN folder
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\04\04"))
File.Copy(file, Path.Combine(subfolder1, Path.GetFileName(file)), true);
// creates CUT SHEET folder under the DESIGN folder
string subfolder1a = Path.Combine(subfolder1, (ProjNo + "_Cut Sheets"));
Directory.CreateDirectory(subfolder1a);
// Creates the ELEC Folder under the DESIGN folder
string subfolder1b = Path.Combine(subfolder1, (ProjNo + "_Elec"));
Directory.CreateDirectory(subfolder1b);
// creates the FIREP Folder under the DESIGN folder
string subfolder1c = Path.Combine(subfolder1, (ProjNo + "_FireP"));
Directory.CreateDirectory(subfolder1c);
// creates the MECH Folder under the DESIGN folder
string subfolder1d = Path.Combine(subfolder1, (ProjNo + "_Mech"));
Directory.CreateDirectory(subfolder1d);
// this copys the file to the MECH Folder under the DESIGN folder
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\05\01"))
File.Copy(file, Path.Combine(subfolder1d, Path.GetFileName(file)), true);
// creates the PLUMBING Folder under the DESIGN folder
string subfolder1e = Path.Combine(subfolder1, (ProjNo + "_Plumbing"));
Directory.CreateDirectory(subfolder1e);
// creates the QA QC Checklists Folder under the DESIGN folder
string subfolder1f = Path.Combine(subfolder1, (ProjNo + "_QA QC Checklist"));
Directory.CreateDirectory(subfolder1f);
// this creates the ELEC folder under QAQC folder under the DESIGN folder
string subfolder1fa = Path.Combine(subfolder1f, "Elec");
Directory.CreateDirectory(subfolder1fa);
// this copys the files to the ELEC folder under QAQC under the DESIGN folder
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\01\01"))
File.Copy(file, Path.Combine(subfolder1fa, Path.GetFileName(file)), true);
Directory.CreateDirectory(subfolder1fa);
// this creates the MECH folder under QAQC under the DESIGN folder
string subfolder1fb = Path.Combine(subfolder1f, "Mech");
Directory.CreateDirectory(subfolder1fb);
// this copys the files to the MECH folder under QAQC
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\01\03"))
File.Copy(file, Path.Combine(subfolder1fb, Path.GetFileName(file)), true);
// this creates the STRUCT folder under QAQC
string subfolder1fc = Path.Combine(subfolder1f, "Struct");
Directory.CreateDirectory(subfolder1fc);
// this copys the files to the STRUCT folder under QAQC
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\01\05"))
File.Copy(file, Path.Combine(subfolder1fc, Path.GetFileName(file)), true);
// creates the Struct Folder
string subfolder1g = Path.Combine(subfolder1, (ProjNo + "_Struct"));
Directory.CreateDirectory(subfolder1g);
// this copys the files to the STRUCT Folder
foreach (var file in Directory.GetFiles(@"X:\2019\Do Not Delete\05\02"))
File.Copy(file, Path.Combine(subfolder1g, Path.GetFileName(file)), true);
// this creates the SOILS folder under the Struct Folder
string subfolder1ga = Path.Combine(subfolder1g, "Soils");
Directory.CreateDirectory(subfolder1ga);
// creates the Tech Folder
string subfolder1h = Path.Combine(subfolder1, (ProjNo + "_Tech"));
Directory.CreateDirectory(subfolder1h);
////////////////////////////////////////////////////////////////////////////////////////////////
// this creates the DWG folder
string subfolder2 = Path.Combine(PathString, (ProjNo + "_Dwg"));
Directory.CreateDirectory(subfolder2);
// creates the ARCH folder under the DWG folder
string subfolder2a = Path.Combine(subfolder2, "Arch");
Directory.CreateDirectory(subfolder2a);
// creates the ZIPS folder under the ARCH folder under the DWG folder
string subfolder2aa = Path.Combine(subfolder2a, "Zips");
Directory.CreateDirectory(subfolder2aa);
// creates the CIVIL Folder under the ARCH folder under the DWG folder
string subfolder2b = Path.Combine(subfolder2, "Civil");
Directory.CreateDirectory(subfolder2b);
// creates the REVIT FAMALIES Folder under the ARCH folder under the DWG folder
string subfolder2c = System.IO.Path.Combine(subfolder2, "Revit Families");
System.IO.Directory.CreateDirectory(subfolder2c);
// creates the REVIT LINKS Folder under the ARCH folder under the DWG folder
string subfolder2d = System.IO.Path.Combine(subfolder2, "Revit Links");
System.IO.Directory.CreateDirectory(subfolder2d);
////////////////////////////////////////////////////////////////////////////////////////////////
// this creates the EMAIL folder
string subfolder3 = System.IO.Path.Combine(PathString, (ProjNo + "_Email"));
System.IO.Directory.CreateDirectory(subfolder3);
////////////////////////////////////////////////////////////////////////////////////////////////
//this creates the FINANCIALS folder
string subfolder4 = System.IO.Path.Combine(PathString, (ProjNo + "_Financials"));
System.IO.Directory.CreateDirectory(subfolder4);
// this copys the files to the Financials folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\02\01"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder4, System.IO.Path.GetFileName(file)), true);
////////////////////////////////////////////////////////////////////////////////////////////////
// this creates the MEETING NOTES Folder
string subfolder5 = System.IO.Path.Combine(PathString, (ProjNo + "_Meeting Notes"));
System.IO.Directory.CreateDirectory(subfolder5);
////////////////////////////////////////////////////////////////////////////////////////////////
// this creates the PHOTOS folder
string subfolder6 = System.IO.Path.Combine(PathString, (ProjNo + "_Photos"));
System.IO.Directory.CreateDirectory(subfolder6);
///////////////////////////////////////////////////////////////////////////////////////////////
// this creates the RFI Folder
string subfolder7 = System.IO.Path.Combine(PathString, (ProjNo + "_RFI"));
System.IO.Directory.CreateDirectory(subfolder7);
// this copys the files to the RFI folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\03\01"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder7, System.IO.Path.GetFileName(file)), true);
//////////////////////////////////////////////////////////////////////////////////////////////
// this creates the SHOPDWGS folder
string subfolder8 = System.IO.Path.Combine(PathString, (ProjNo + "_ShopDwgs"));
System.IO.Directory.CreateDirectory(subfolder8);
// this copys the files to the SHOPDWGS folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\05\03"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder8, System.IO.Path.GetFileName(file)), true);
//////////////////////////////////////////////////////////////////////////////////////////////
// this creates the SUBMITTAL folder
string subfolder9 = System.IO.Path.Combine(PathString, (ProjNo + "_Submittal"));
System.IO.Directory.CreateDirectory(subfolder9);
// creates the ~MOST CURRENT DRAWINGS folder under the SUBMITTAL folder
string subfolder9a = Path.Combine(subfolder9, "~Most Current Drawings");
Directory.CreateDirectory(subfolder9a);
//////////////////////////////////////////////////////////////////////////////////////////////
// this creates the WORDPROC folder
string subfolder10 = System.IO.Path.Combine(PathString, (ProjNo + "_WordProc"));
System.IO.Directory.CreateDirectory(subfolder10);
// this creates the SITE VISIT Folder under the WORDPROC folder
string subfolder10a = System.IO.Path.Combine(subfolder10, "Site Visit");
System.IO.Directory.CreateDirectory(subfolder10a);
// this copys the files to the SITE VISIT folder under the WORDPROC folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\04\05"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder10a, System.IO.Path.GetFileName(file)), true);
// this creates the SITE VISIT Folder under the WORDPROC folder
string subfolder10b = System.IO.Path.Combine(subfolder10, "Site Visit Reports");
System.IO.Directory.CreateDirectory(subfolder10b);
// this creates the Special Inspections Folder
string subfolder10c = System.IO.Path.Combine(subfolder10, "Special Inspections");
System.IO.Directory.CreateDirectory(subfolder10c);
// this copys the files to the Special Inspections folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\05\05"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder10c, System.IO.Path.GetFileName(file)), true);
// this creates the Specs folder
string subfolder10d = System.IO.Path.Combine(subfolder10, "Specs");
System.IO.Directory.CreateDirectory(subfolder10d);
// this copys the files to the specs folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\05\06"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder10d, System.IO.Path.GetFileName(file)), true);
// this creates the Status Folder
string subfolder10e = System.IO.Path.Combine(subfolder10, "Status");
System.IO.Directory.CreateDirectory(subfolder10e);
// this creates the Transmittals folder
string subfolder10f = System.IO.Path.Combine(subfolder10, "Transmittals");
System.IO.Directory.CreateDirectory(subfolder10f);
// this copys the files to the transmittals folder
foreach (var file in System.IO.Directory.GetFiles(@"X:\2019\Do Not Delete\04\02"))
System.IO.File.Copy(file, System.IO.Path.Combine(subfolder10f, System.IO.Path.GetFileName(file)), true);
//////////////////////////////////////////////////////////////////////////////////////////////
// this clears the form
// First the textbox
textBox1.Text = null;
// then the radio buttons
var cntls = GetAll(this, typeof(RadioButton));
foreach (Control cntrl in cntls)
{
RadioButton _rb = (RadioButton)cntrl;
if (_rb.Checked)
{
_rb.Checked = false;
}
}
// this ends the activity and lets you know the folders were created
MessageBox.Show("The Project Folders were created, Thank You!");
}
private void button2_Click(object sender, EventArgs e)
{
//////////////////////////////////////////////////////////////////////////////////////////
// This is for the preview
// this sets the string for location and clears it
string location = null;
// get the current year
string year = DateTime.Now.Year.ToString();
// these are the radio buttons
if (radioButton1.Checked)
{
location = @"\\mmejaxdc1\projects\" + year +"\\";
}
else if (radioButton2.Checked)
{
location = @"\\mmeatldc\projects\" + year + "\\";
}
else if (radioButton3.Checked)
{
location = @"\\mmecltdc\projects\" + year + "\\";
}
else if (radioButton4.Checked)
{
location = @"\\mmecltdc\raleigh\projects\" + year + "\\";
}
else
{
MessageBox.Show("No Location was Selected!");
return;
}
// this reads the radio buttons and sets a location, then shows the preview of the path
// based on the selection
var tempproj = textBox1.Text;
string FullPath = location + tempproj;
MessageBox.Show(FullPath);
// this ends the preview code users closes messagebox to end
///////////////////////////////////////////////////////////////////////////////////////
}
private void button3_Click(object sender, EventArgs e)
{
// this clears the form
// textbox
textBox1.Text = null;
// radio buttons
var cntls = GetAll(this, typeof(RadioButton));
foreach (Control cntrl in cntls)
{
RadioButton _rb = (RadioButton)cntrl;
if (_rb.Checked)
{
_rb.Checked = false;
}
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
this.TopMost = checkBox1.Checked;
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrls => GetAll(ctrls, type)).Concat(controls).Where(c => c.GetType() == type);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
我尝试过:
string newProjectNumber = PathString; //您要添加的新项目编号 DirectoryInfo d = new DirectoryInfo(project); //设置文件夹路径 DirectoryInfo []目录= d.GetDirectories(); //获取路径中目录的完整列表
foreach (DirectoryInfo dir in dirs) // iterating over dirs
{
string dirName = dir.Name;
if (dirName.Substring(0, 6) == newProjectNumber)
{
Console.WriteLine("This folder already exists.");
}
}
Console.ReadLine();
答案 0 :(得分:1)
您可以这样做:
class Program
{
static void Main(string[] args)
{
string newProjectNumber = "000006"; // new project number you want to add
DirectoryInfo d = new DirectoryInfo(@"C:\Test");//Assuming Test is your Folder
DirectoryInfo[] dirs = d.GetDirectories(); // get the full list of dirs inside C:\Test
foreach (DirectoryInfo dir in dirs) // iterating over dirs
{
string dirName = dir.Name;
if (dirName.Substring(0,6) == newProjectNumber)
{
Console.WriteLine("This folder already exists.");
}
}
Console.ReadLine();
}
}
答案 1 :(得分:0)
Directory.EnumerateDirectories(string, string)
函数可用于在目录中搜索与模式匹配的文件夹。此功能演示了适用于您的方案的一个示例:
static bool DirectoryWithPrefixExists(string root, string prefix)
{
return Directory.EnumerateDirectories(root, prefix + "*").Any();
}
bool existsAlready = DirectoryWithPrefixExists(@"C:\root\directory", projectNumber);