繁体   English   中英

如何从savefiledialog获取完整路径并在“startInfo.Arguments”中使用?

[英]How to get full path from savefiledialog and use in “startInfo.Arguments”?

在我的情况下, SaveFileDialog不会写任何文件,但我想用来指定命令行应用程序的路径,该应用程序将在sf对话框中的“已保存”的相同位置创建日志文件。

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFileName(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFileName(sfd.FileName);

您可以使用

Path.GetFullPath(sfd.FileName);

代替

Path.GetFileName(sfd.FileName);

完整版......

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.txt";
string sfdname = saveFileDialog1.FileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
  Path.GetFullPath(sfd.FileName);
}

startInfo.Arguments = "--log=" + Path.GetFullPath(sfd.FileName);

只需删除Path.GetFileName

startInfo.Arguments = "--log=\"" + sfd.FileName + "\"";

我认为你根据你所描述的内容使用了错误的对话框。

尝试使用FolderBrowserDialog类:

string folderPath = string.Empty;

using (FolderBrowserDialog fdb = new FolderBrowserDialog()) {
  if (fdb.ShowDialog() == DialogResult.OK ){
    folderPath = fdb.SelectedPath;
  }
}

if (folderPath != string.Empty) {
  startInfo.Arguments = "--log=" + folderPath;
}

问题可能是使用了错误的FileSaveDialog
Win32.dll中的Win32.dll不提供完整路径,但System.Windows.Forms中的Win32.dll提供完整路径。

OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "Image files | *.jpg";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                 employee_dp.Image = Image.FromFile(openFileDialog1.FileName);
                 string path = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                 string onlyFileName = System.IO.Path.GetFileName(openFileDialog1.FileName);
                 filepath = Path.GetFullPath(path).Replace(@"\", @"\\");
                 filepath = filepath + "\\\\" + onlyFileName;
                 MessageBox.Show(filepath);

也许Path.GetFullPath会有帮助吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM