我正在尝试处理如果我想要创建的文件夹已经存在..添加一个数字到文件夹名称..像Windows资源管理器..例如(新文件夹,新文件夹1,新文件夹2 ..)我怎么能以递归方式做到我知道这段代码是错误的.我该如何修复或改变下面的代码来解决问题?
int i = 0;
private void NewFolder(string path)
{
string name = "\\New Folder";
if (Directory.Exists(path + name))
{
i++;
NewFolder(path + name +" "+ i);
}
Directory.CreateDirectory(path + name);
}
Run Code Online (Sandbox Code Playgroud) 我可以使用out递归方法的参数吗?如果可能,我如何使用以下代码执行此操作?
private void PrepareDir(out List<_File> listFiles,string root,string dirPath) {
DirectoryInfo dirRoot = new DirectoryInfo(dirPath);
FileInfo [] Files = dirRoot.GetFiles();
dirPath = dirPath.Substring(root.Length);
foreach (FileInfo file in Files) {
_File _file = new _File();
_file.Name = dirPath + "\\" + file.Name;
_file.Path = file.FullName;
_file.Size = file.Length;
listFiles.Add(_file);
}
foreach (DirectoryInfo dir in dirRoot.GetDirectories()) {
PrepareDir(out listFiles, root, dir.FullName);
}
}
private void btnButton1_Click(object sender, EventArgs e) {
List<_File> Files = new List<_File>();
PrepareDir(out Files,currAddress, currAddress);
}
Run Code Online (Sandbox Code Playgroud) 我试图从WCF服务获取一个Image
我有一个OperationContract函数,它返回一个Image到客户端,
但是当我从客户端调用它时,我得到这个异常: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9619978'.
客户:
private void btnNew_Click(object sender, EventArgs e)
{
picBox.Picture = client.GetScreenShot();
}
Run Code Online (Sandbox Code Playgroud)
Service.cs:
public Image GetScreenShot()
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bmp = new Bitmap(bounds.Width,bounds.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
using (MemoryStream ms …Run Code Online (Sandbox Code Playgroud) 
考虑到这是一个显示文件和文件夹的 ListView,我已经为复制/移动/重命名/显示属性等编写了代码,我只需要最后一件事。如何像在 Windows 资源管理器中一样在同一个 ListView 中拖放,我有移动和复制功能,我只需要获取用户在某个文件夹中放置的项目或以其他方式我需要获取这两个参数来调用复制功能
void copy(ListViewItem [] droppedItems, string destination path)
{
// Copy target to destination
}
Run Code Online (Sandbox Code Playgroud) 我知道这不是关于取消BackGroundWorker的第一个问题,但我找不到解决问题的答案.
我有一个发送文件的方法..即时通讯使用backgroundworker来调用它..
所以如何在发送文件的中间取消backgroundworker ..我的意思是我应该放在哪里
if (backgroundWorker1.CancellationPending == true)
{
e.Cancel = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
这是代码:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
List<object> job = (List<object>)e.Argument;
string srcPath = (string)job[0];
string destPath = (string)job[1];
SendFile(srcPath, destPath);
}
Run Code Online (Sandbox Code Playgroud)
发送方式:
private void SendFile(string srcPath, string destPath)
{
string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
{
try
{
long fileSize = fs.Length;
if (sizeAll == 0)
sizeAll = fileSize;
sum = 0;
int count = 0;
data = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个服务器客户端应用程序.Yhe服务器侦听某个端口(例如:9090).但我很困惑:
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9090);
server.Bind(ep);
server.Listen(100);
Run Code Online (Sandbox Code Playgroud)
我正在构建一个WCF服务,在客户端连接到此服务后,当服务断开连接时,客户端没有注意到,并且它不会触发任何操作,我想在连接丢失时关闭客户端表单,那么如何从客户端检测WCF服务断开或关闭.
我正在尝试为垂直和水平滚动方向创建两个可观察的滚动事件。
我尝试使用pairwise()和bufferCount(2,1)运算符从水平滚动事件中过滤垂直滚动事件,但问题是获取prev.scrollTop和 的重复值curr.scrollTop
import { Component, ViewChild, AfterViewInit, ElementRef } from '@angular/core';
import { fromEvent } from 'rxjs';
import { pairwise, tap, filter } from 'rxjs/operators';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
@ViewChild('scrollable', {static: false}) scrollable: ElementRef;
ngAfterViewInit() {
fromEvent(this.scrollable.nativeElement, 'scroll').pipe(
pairwise(),
tap(([prev, curr]) => console.log(prev.target.scrollTop, curr.target.scrollTop)),
filter(([prev, curr]) => prev.target.scrollTop !== curr.target.scrollTop),
tap((e) => console.log(e)) // <= Never reached
).subscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个连接到WCF服务的客户端应用程序,我从服务器获取文件大小作为long值然后我在客户端转换它string所以它看起来像ex:52.21 MB
每次用户更改当前目录路径时,应用程序都会获得太多文件大小.
我的问题:我应该值转换为string从格式WCF服务的应用程序,然后将其返回到客户端的string,或者我应该只返回大小为long价值,让客户将其转换为一个string格式
换句话说,哪个值在内存中占用更多字节:
long size = 55050240;
string size = "52.5 MB";
long large_size = 56371445760;
string large_size = "52.5 GB";
Run Code Online (Sandbox Code Playgroud)
我使用此方法将long值转换为字符串格式:
private string ConvertUnit(long source)
{
const int byteConversion = 1024;
double bytes = Convert.ToDouble(source);
if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
{
return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
}
else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range …Run Code Online (Sandbox Code Playgroud) 我正在尝试将listViewGridView 更改为IconView,如截图中所示,每个项目都在进行.

风格XAML
<UserControl.Resources>
<Style x:Key="FileItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Width" Value="50"/>
<Setter Property="Margin" Value="5,5,5,5"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" Width="50">
<Border x:Name="border" BorderBrush="{x:Null}" BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2.5"/>
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ContentPresenter />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
ListView XAML
<ListView ItemContainerStyle="{DynamicResource FileItemStyle}" HorizontalAlignment="Left" Height="194.667" Margin="11,77.666,0,0" VerticalAlignment="Top" Width="368.667" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" UseLayoutRounding="False">
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Height="50">
<Border x:Name="border" BorderBrush="{x:Null}" BorderThickness="1" HorizontalAlignment="Stretch" Height="50" VerticalAlignment="Stretch" Width="50" CornerRadius="2.5"/> …Run Code Online (Sandbox Code Playgroud)