Ich habe eine C# -Anwendung, in der ich versuche, eine Datei mit einem benutzerdefinierten Dateikopier von einem Ort zu einem anderen zu kopieren (siehe Dateikopie mit Fortschrittsleiste). Hier ist mein Code: < /p>
namespace FileCopier
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
string strSourceFileName = string.Empty;
string strOutFilePath = string.Empty;
string strOutFileName = string.Empty;
private void btnSelectInput_Click(object sender, EventArgs e)
{
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
ofd.Title = "Select a file to copy...";
if (ofd.ShowDialog() == DialogResult.OK)
{
//strSourceFileName = ofd.FileName;
strSourceFileName = ofd.FileName.Trim(); // Remove any leading or trailing whitespace characters.
txtInputFile.Text = strSourceFileName; // Display the selected file path in the textbox.
}
if (strSourceFileName == String.Empty)
{
return; // User didn't select a file to open.
}
}
private void btnSelectOutput_Click(object sender, EventArgs e)
{
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
sfd.Title = "Type the name for the new file... Any existing file will be overwritten.";
strOutFileName = ofd.SafeFileName;
sfd.FileName = strOutFileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
txtOutputFile.Text = sfd.FileName;
strOutFilePath = txtOutputFile.Text;
}
if (strOutFileName == String.Empty)
{
return; // User didn't select a file to open.
}
}
private void btnCopyFile_Click(object sender, EventArgs e)
{
if (File.Exists(strSourceFileName))
{
if (File.Exists(strOutFileName))
{
DialogResult result = MessageBox.Show("The file already exists. Do you want to overwrite it?", "File Exists", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
return; // User chose not to overwrite the existing file.
}
if (result == DialogResult.Yes)
{
File.Delete(strOutFileName); // Delete the existing file if the user chooses to overwrite it.
}
}
backgroundWorker1.RunWorkerAsync(); // Start the background worker to copy the file.
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
CustomFileCopier cfc = new CustomFileCopier(strSourceFileName, strOutFileName);
cfc.Copy(); // Start the file copy operation.
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbCopyProgress.Value = e.ProgressPercentage;
lblProgress.Text = e.ProgressPercentage.ToString() + "%";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
private void frmMain_Load(object sender, EventArgs e)
{
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
}
}
< /code>
Ich kann eine Eingabedatei auswählen (z. B. eine Datei im Ordner Downloads) und dann eine Ausgabedateisposition (z. B. den Desktop) auswählen. Anstatt die Datei auf den Desktop zu kopieren (oder in welchem Ordner ich ausgewählt habe), kopiert sie die Datei in den Ordner "C: \ Benutzer \ Source \ Repos \ FileCopier \ FileCopier \ bin \ Debug". Ich kann nicht herausfinden, wie ich es auf den richtigen Ordner einstellen kann.>
Das Kopieren der Datei in Anwendung kopiert die Datei anstelle des ausgewählten Ordners in den Debug -Ordner [geschlosse ⇐ C#
-
- Similar Topics
- Replies
- Views
- Last post