我有简单而愚蠢的问题但是我希望找到一个能让我清楚了解C#refrence对象的答案,以及哪里是即时通讯的最佳位置。我已宣布openfile
可变后阶段宣言,以便我可以从课堂内的任何其他方法引用它。每一件事都可以正常工作,直到我按下添加照片按钮并选择一张照片,如果我再次按下按钮,取消了显示对话框,openfile.FileName
未保存文件扩展名。为了解决这个问题,我确实将这行代码openfile = new OpenFileDialog();
移到了类构造函数中。以这种方式即时关联对象是错误的,它将在RAM中保存空格。或者只在你所在的位置即时对象打算用它。
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
.
.
.
答案 0 :(得分:0)
如果要在任何地方访问文件名而不是空,请指定本地更改并在选择叔叔时保留地址。 在命名空间之后
private const string FileName = string.Empty;
所以你的代码
private void addPhotoBtn_Click(object sender, RoutedEventArgs e)
{
openfile = new OpenFileDialog();
openfile.Filter = "Images |*.JPG; *.PNG";
if (openfile.ShowDialog() == true)
{
userImage.Source = new BitmapImage(new Uri(openfile.FileName, UriKind.RelativeOrAbsolute));
filename = openfile.SafeFileName;
sourcePath = @openfile.FileName;
this.FileName = @openfile.FileName;
targetPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "/Images/users/" + getTime + System.IO.Path.GetExtension(openfile.FileName);
}
}
private void saveUserBtn_Click(object sender, RoutedEventArgs e)
{
string fname, lname, username, password, email, phone, address, role, photo = "";
int spec_id; int dept_id;
fname = firstNameTextBox.Text.ToString();
lname = lastNameTextBox.Text.ToString(); ;
username = usernameTextBox.Text.ToString(); ;
password = passwordBox.Password;
email = emailTextBox.Text.ToString(); ;
phone = phoneTextBox.Text.ToString(); ;
address = addressTextBox.Text.ToString();
spec_id = specComboBox.SelectedIndex;
role = roleComboBox.Text.ToString();
if (deptComboBox.SelectedIndex >= 0)
dept_id = int.Parse(deptComboBox.SelectedValue.ToString());
else
dept_id = 0;
// Input Validation
if (state == "add" && sourcePath != null || sourcePath != null && state != "add")
{
photo = new Uri("pack://application:,,/CMMS;component/images/users/") + getTime + System.IO.Path.GetExtension(this.FileName);
}
.
.
.