C# Powershell GUI如何使用选定文件夹并将其项目复制到另一个驱动器中的其他文件夹
使用powershell和XAML,我希望用户选择所选文件夹,然后将内容从其中一个子文件夹(如子文件夹1)复制到不同驱动器(如C驱动器)中的另一个目录。一切正常,除了我的评论。代码说明:用户单击“选择”按钮,然后选择所需的文件夹。此文件夹路径显示在名为sourcePath的列表框中。接下来,用户在文本框中的directoryName中键入所需的名称,如folderA。当用户按下“创建自”按钮时,如果folderA存在,将使用“删除项C:\$var-Recurse-Force”删除该文件夹,并使用“复制项-Path C:\$a\subfolder1 C:\$var-Recurse”再次创建该文件夹。最后,使用copyitem-Path C:\$a C:\$var-Recurse,我希望将子文件夹1的内容复制到这个文件夹中C# Powershell GUI如何使用选定文件夹并将其项目复制到另一个驱动器中的其他文件夹,c#,wpf,xaml,powershell,C#,Wpf,Xaml,Powershell,使用powershell和XAML,我希望用户选择所选文件夹,然后将内容从其中一个子文件夹(如子文件夹1)复制到不同驱动器(如C驱动器)中的另一个目录。一切正常,除了我的评论。代码说明:用户单击“选择”按钮,然后选择所需的文件夹。此文件夹路径显示在名为sourcePath的列表框中。接下来,用户在文本框中的directoryName中键入所需的名称,如folderA。当用户按下“创建自”按钮时,如果folderA存在,将使用“删除项C:\$var-Recurse-Force”删除该文件夹,并使用
$XAML = @'
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Folder-Browser"
Height="500"
Width="600"
>
<Grid>
<Button Name="create" Content="create" HorizontalAlignment="Left"
Margin="158,175,0,0" VerticalAlignment="Top" Width="121" />
<TextBox Name="directoryName" HorizontalAlignment="Left" Height="23"
Margin="33,175,0,0" TextWrapping="Wrap" Text="directoryName"
VerticalAlignment="Top" Width="120"/>
<Button Name="choose" Content="choose" HorizontalAlignment="Left"
Margin="32,47,0,0" VerticalAlignment="Top" Width="121" />
<ListBox Name="sourcePath" HorizontalAlignment="left" Height="45"
Margin="32,87,0,0" VerticalAlignment="Top" Width="430"/>
<Button Name="copyItems" Content="copy-items" HorizontalAlignment="Left"
Margin="284,176,0,0" VerticalAlignment="Top" Width="121"
AutomationProperties.Name="copyItems" />
</Grid>
</Window>
'@
#Parse XAML
$Win = [Windows.Markup.XamlReader]::Parse($XAML)
# Define variables
$sourcePath = $Win.FindName("sourcePath")
$choose= $Win.FindName("choose")
$directoryName=$Win.FindName("directoryName")
$create=$Win.FindName("create")
$copyItems=$Win.FindName("copyItems")
#Event Handling
$choose.Add_Click({Select-FolderDialog})
# $a =$objForm.SelectedPath()
Add-Type -Assembly System.Windows.forms
$create.Add_Click({
$script:var = $directoryName.Text.ToString()
Remove-Item C:\$var -Recurse -Force
new-item c:\$var -itemType directory
# cannot achieve copy the contents from the "SelectedFolder\subfolder1" onto
# the new "directoryName" created.
# Copy-Item -Path C:\"$a"\subfolder1 C:\$var -Recurse
})
# Functions
# "Select-FolderDialog" is used for the "choose" button
function Select-FolderDialog
{
param([String]$Description="Select Folder",
[String]$RootFolder="Desktop")
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$Show = $objForm.ShowDialog()
if ($Show -eq "OK")
{
$SourcePath.Items.Add($objForm.SelectedPath)
}
}
$Win.ShowDialog()
在您有线路的地方:
Copy-Item -Path C:\"$a"\subfolder1 C:\$var -Recurse
为了获得完整的文件夹路径,我将替换为:
Copy-Item -Path "$($sourcePath.SelectedValue)\subfolder1" C:\$var -Recurse
请注意,额外的$sourcePath.SelectedValue会强制PowerShell将其作为一个整体进行评估。
此外,在PowerShell中,您可以将变量放在引号内。其中有一行:
Copy-Item -Path C:\"$a"\subfolder1 C:\$var -Recurse
为了获得完整的文件夹路径,我将替换为:
Copy-Item -Path "$($sourcePath.SelectedValue)\subfolder1" C:\$var -Recurse
请注意,额外的$sourcePath.SelectedValue会强制PowerShell将其作为一个整体进行评估。
此外,在PowerShell中,您可以在引号中添加变量。谢谢您,斯拉夫。我重写了我的问题描述。希望它能被理解好。再次感谢你,斯拉夫人。成功了。还有一个问题:如何从$sourcePath.SelectedValue复制所有内容。我使用了Copy Item-Path$sourcePath.SelectedValue-Destination$directoryName.Text-Recurse,但没有复制所选子文件夹中的内容。我甚至尝试添加“*”,比如复制项-Path$sourcePath.SelectedValue'\'*-Destination$directoryName.Text,我想你差不多做到了。尝试删除单引号。例如$$sourcePath.SelectedValue\*谢谢@slavs。你太棒了。是的,成功了。我添加了复制项-Path$$sourcePath.SelectedValue*C:\$var-Recurse@BenQ很乐意帮忙。如果这个答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这么做。谢谢你,斯拉夫人。我重写了我的问题描述。希望它能被理解好。再次感谢你,斯拉夫人。成功了。还有一个问题:如何从$sourcePath.SelectedValue复制所有内容。我使用了Copy Item-Path$sourcePath.SelectedValue-Destination$directoryName.Text-Recurse,但没有复制所选子文件夹中的内容。我甚至尝试添加“*”,比如复制项-Path$sourcePath.SelectedValue'\'*-Destination$directoryName.Text,我想你差不多做到了。尝试删除单引号。例如$$sourcePath.SelectedValue\*谢谢@slavs。你太棒了。是的,成功了。我添加了复制项-Path$$sourcePath.SelectedValue*C:\$var-Recurse@BenQ很乐意帮忙。如果这个答案已经解决了你的问题,请通过点击复选标记来考虑。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉。没有义务这样做。