How to create many folders in a sub-folder
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Progressbar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = #"A:\DMS\SCOSTADL";
try
{
// Determine whether the directory exists.
if (Directory.Exists(path))
{
MessageBox.Show("Paths Exists already!!!!!!!!!");
return;
}
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(path);
MessageBox.Show("Directory Created Successfully....!");
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally { }
}
}
}
Here in this example i am able create a folder(DMS) and sub-folder(SCOSTADL) and i want to create many sub-folders along with Sub-folder(SCOSTADL).please give me suggestions......
CreateDirectory creates all sub-directories in the path as well, so you can do Directory.CreateDirectory("C:\\abc\\def\\ghi");.
Related
Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
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.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
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.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1
Hello I am working on c# simple project.
This is the code:
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.Threading;
using Microsoft.Win32;
using System.IO;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath);
else
rk.DeleteValue(AppName,false);
}
namespace web1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// open browser
Thread.Sleep(6000);
System.Diagnostics.Process.Start("https://google.com/");
}
}
}
It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"
I want to:
If I click on this program .exe, it should run on the startup (every startup).
How to deal with it? I've tried so many ideas, but doesn't helped.
Thanks in advance.
BTW: it's my first time here, nice to meet you all!
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.Net;
namespace Garfield
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "e.gif");
}
public void download(string link, string name)
{
using (WebClient Client = new WebClient())
{
Client.DownloadFile(link, name);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
Using the code above I downloaded an image online, now how do I set the location of that file to somewhere else instead of it being in the debug folder?
This can be done through mentioning a valid desired location and name when download.
Client.DownloadFile("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1",
#"C:\yourfolder\yourfilename.png");
You can pass an entire path to your download method:
download("https://d1ejxu6vysztl5.cloudfront.net/comics/garfield/2019/2019-11-25.gif?v=1.1", "C:\\Users\\(You)\\Documents\\e.gif");
Just replace everything before "e.gif" with the path you desire.
See more here: https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile?view=netframework-4.8
I want to make a program to read the total number of drives in system and make list view to show its information that is, counting the number of folders and files individually in each drive present and there is some error due to which I am not able to solve that problem. Please help me in finding the error in the same.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using SystemInformation;
using SystemInformation.Properties;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.IO;
namespace SystemInformation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
ListViewItem item = new ListViewItem();
var k = Console.WriteLine(drive.Name);
item.SubItems.Add(k);
foreach (string path in Directory.EnumerateFiles(drive.Name))
{
Console.WriteLine(path);
lstInfo.Items.Add(item);
}
}
}
catch (MySqlException ex)
{
Trace.WriteLine(ex.Message);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
public string k { get; set; }
}
}
First of all, the method Console.WriteLine() is void and you can't get an output from it.
You can't do multiple levels using listview. You will need to use TreeView instead.
The following code will give you the file structure on you computer:
private void Form1_Load(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
var item = TV_Items.Nodes.Add(drive.Name);
AddDirectories(item, drive.Name);
}
}
private void AddDirectories(TreeNode parent, string path)
{
var directories = Directory.GetDirectories(path);
foreach (var directory in directories)
{
try
{
var subItem = parent.Nodes.Add(Path.GetDirectoryName(directory));
foreach (var file in Directory.GetFiles(directory))
{
subItem.Nodes.Add(Path.GetFileNameWithoutExtension(file));
}
AddDirectories(subItem, directory);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
I didn't understand what do you want to count exactly. Is it the number of folders and files in each folder or just in each drive?
i have created a form for a codeDOM compiler and it can compile my code if its in a single text file but i want to be able to compile the source code in the text file and a class thats in a text file so they can work together.
I am unsure how to code codeDOM to add my class file as a resource for the main source to be compiled
Heres what i have so far
codeDOM Compiler
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
namespace CodeDOMSourceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnCompile_Click(object sender, EventArgs e)
{
CompilerParameters Params = new CompilerParameters();
Params.GenerateExecutable = true;
Params.ReferencedAssemblies.Add("System.dll");
Params.ReferencedAssemblies.Add("TextFile1.txt");
Params.OutputAssembly = "output.exe";
string Source = Properties.Resources.CodeDOMSource;
Source = Source.Replace("[TEXT]", txtReplace.Text);
CompilerResults Results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);
if (Results.Errors.Count > 0)
{
foreach (CompilerError err in Results.Errors)
Console.WriteLine(err.ToString());
}
else Console.WriteLine("Compiled just fine!");
}
}
}
Source file
namespace testingCodeDOM
{
class Program
{
static void Main()
{
System.Console.WriteLine("[TEXT]");
Console.WriteLine(Testing.textwork());
System.Console.Read();
}
}
}
ClassFile
namespace testingCodeDOM
{
class Testing
{
public static string textwork()
{
string hello = "calss worked";
return hello;
}enter code here
}
}
Any ideas how to do this because i have googled it an am getting no were or at least nothing i understand
on a side not this works with with just the source but am trying to adapt it to use class files aswell
The CompileAssemblyFromSource() method can take any number of source code strings (since it's a params method). So, you can call it something like:
CompileAssemblyFromSource(Params, Source, ClassFile)
Where ClassFile is a string that contains the text of the second file.