18. marts 2005 - 14:41
Der er
11 kommentarer og 1 løsning
Kopier mappe
Jeg skal kopiere en mappe samt alt indholdet over i en anden. Kan kun finde ud af at flytte hele mappen. Det gør jeg med Directory.Move(path, target); men kan ikke finde noget til at kopiere.
Annonceindlæg tema
Forsvar & beredskab
Cybersikkerhed, realtidsdata og robuste it-systemer er blevet fundamentet for moderne forsvar.
18. marts 2005 - 14:46
#1
Jeg mener at du bliver nødt til at lave noget rekursivt som kopierer de enkelte filer.
18. marts 2005 - 14:49
#2
Kode skitse: public void XCopy(string dir1, string dir2) { string[] files = Directory.GetFiles(dir1); foreach (string f in files) { File.Copy(f, dir2 + f.Substring(dir1.Length), true); } string[] dirs = Directory.GetDirectories(dir1); foreach (string d in dirs) { XCopy(d, dir2 + d.Substring(dir1.Length)); } }
18. marts 2005 - 14:58
#3
Jeg er ikke så god til det her, så kan ikke rigtigt se hvor din kode skal placeres?
18. marts 2005 - 15:02
#4
Det er en metode som skal sættes ind i din klasse
18. marts 2005 - 15:03
#5
Nu er jeg endnu mere forvirret:D
18. marts 2005 - 15:09
#6
Prøv lige at post noget af den kode du har. (vi ved jo ikke engang om det er en console app eller en windows app eller en ASP.NET side)
18. marts 2005 - 15:19
#7
Har sådan set ikke lavet noget... using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using Microsoft.Win32; using System.Runtime.InteropServices; namespace DefaultProfile { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 56); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "kopier"; // // button2 // this.button2.Location = new System.Drawing.Point(104, 96); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "Luk"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } } }
18. marts 2005 - 21:16
#8
Et eksempel: using System; using System.IO; using System.Drawing; using System.Windows.Forms; public class MainForm : Form { private Label status; private Button start; private Button abort; private Timer t; public MainForm() { status = new Label(); start = new Button(); abort = new Button(); SuspendLayout(); status.Location = new Point(50, 50); status.Size = new Size(200, 50); status.Name = "Status TextBox"; status.Font = new Font(FontFamily.GenericSerif, 24); status.Text = "Stopped"; status.ForeColor = Color.Red; start.Location = new Point(50, 150); start.Size = new Size(200, 50); start.Name = "Start Button"; start.Text = "Start"; start.Click += new EventHandler(StartClick); abort.Location = new Point(50, 250); abort.Size = new Size(200, 50); abort.Name = "Abort Button"; abort.Text = "Abort"; abort.Click += new EventHandler(AbortClick); ClientSize = new Size(300, 350); Controls.Add(status); Controls.Add(start); Controls.Add(abort); Name = "Main Form"; Text = "Main Form"; ResumeLayout(false); t = new Timer(); t.Interval = 60 * 1000; t.Tick += new EventHandler(DoIt); } private void XCopy(string dir1, string dir2) { string[] files = Directory.GetFiles(dir1); foreach (string f in files) { File.Copy(f, dir2 + f.Substring(dir1.Length), true); } string[] dirs = Directory.GetDirectories(dir1); foreach (string d in dirs) { XCopy(d, dir2 + d.Substring(dir1.Length)); } } private void DoIt(object sender, EventArgs e) { XCopy("C:\\fra", "C:\\til"); } private void StartClick(object sender, EventArgs e) { t.Start(); status.Text = "Started"; status.ForeColor = Color.Green; } private void AbortClick(object sender, EventArgs e) { t.Stop(); status.Text = "Stopped"; status.ForeColor = Color.Red; } [STAThread()] public static void Main(string[] args) { Application.Run(new MainForm()); Application.Exit(); Environment.Exit(0); } }
21. marts 2005 - 08:48
#9
Jeg er nok lidt en tumpe, men kan stadig ikke få det til at virke:D using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace DefaultProfile { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(104, 56); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "kopier"; // // button2 // this.button2.Location = new System.Drawing.Point(104, 96); this.button2.Name = "button2"; this.button2.TabIndex = 1; this.button2.Text = "Luk"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void XCopy(string dir1, string dir2) { string[] files = Directory.GetFiles(dir1); foreach (string f in files) { File.Copy(f, dir2 + f.Substring(dir1.Length), true); } string[] dirs = Directory.GetDirectories(dir1); foreach (string d in dirs) { XCopy(d, dir2 + d.Substring(dir1.Length)); } } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } } }
21. marts 2005 - 11:54
#10
Hvornår kalder du din XCopy() function? Hvis du i design editoren dobbeltklikker på din "button1" vil den auto-generere en click handler a la: private void button1_Click(object sender, System.EventArgs e) { } tilføj et kald til din XCopy metode inde i den metode krop og giv den nogle værdier således: private void button1_Click(object sender, System.EventArgs e) { this.XCopy("Skriv en sti her","Skriv en anden sti her"); }
21. marts 2005 - 12:08
#11
Ja XCopy skal ihvertfald kaldes. (du behøver ikke nødvendigvis en timer som i mit eksempel - det var til en specifik problem stilling i et andet spørgsmål)
10. april 2005 - 17:40
#12
OK ?
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.