gpt4 book ai didi

c# - 公共(public)字符串的值始终为空

转载 作者:行者123 更新时间:2023-12-05 08:58:19 24 4
gpt4 key购买 nike

我几天前开始学习 C#,但我遇到了公共(public)字符串问题,我目前正在尝试编写一个程序来复制和替换文件以供练习,但无论如何我都遇到公共(public)字符串问题我尝试更改了多少代码我自己也搞不懂所以我来这里寻求帮助

我做错了什么?

代码如下:

namespace Extractor{    public partial class Form1 : Form    {        public string s        {            get;            set;        }        public string sSelectedPath        {            get;            set;        }        public string beckup        {            get;            set;        }        public Form1()        {            InitializeComponent();        }        private void direc_Click(object sender, EventArgs e)        {            FolderBrowserDialog fbd = new FolderBrowserDialog();            fbd.Description = "Select a folder";

if (fbd.ShowDialog() == DialogResult.OK)
{
string sSelectedPath = fbd.SelectedPath;
}

}

private void choof_Click(object sender, EventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.*)|*.*";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = true;

if (choofdlog.ShowDialog() == DialogResult.OK)
{
string s = choofdlog.FileName;
}

}

private void button3_Click(object sender, EventArgs e)
{
ReplaceFile( s, sSelectedPath, beckup);
}

public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace)
{
File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);

}

private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbb = new FolderBrowserDialog();
fbb.Description = "Select a folder";

if (fbb.ShowDialog() == DialogResult.OK)
{
string beckup = fbb.SelectedPath;
}
}
}

最佳答案

if (fbd.ShowDialog() == DialogResult.OK)
{
string sSelectedPath = fbd.SelectedPath;
}

您在这里使用string 关键字声明了一个 变量。它没有设置属性。只需删除 string。例如

if (fbd.ShowDialog() == DialogResult.OK)
{
sSelectedPath = fbd.SelectedPath;
}

关于c# - 公共(public)字符串的值始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24451852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com