本文介绍了单击操作后的Path.getfilename的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有人告诉我如何在用户选择硬盘上的文件后检索文件名和路径吗?



我尝试过的事情:



目前我什么都没有。我正在使用OpenFileDialog对象和SaveFileDialog对象。我在MSDN上读过有关directory.GetFiles但它并不适用于我。事实上,有关GetFiles的所有信息都是针对具有特定字符串的路径

 string [] dirs = Directory.GetFiles(@c:\, c *); 



或拆分路径,或将文件夹从1个目录移动到另一个目录等等...

我的情况除了用户选择的路径外没有任何字符串。

编辑

 private void btnSavechanges_Click(object sender,EventArgs e) )
{
//来自richtextbox的文本
string strsave = richTextBox1.ToString();
//使用(SaveFileDialog sfdSave = new创建一个新的SaveFileDialog对象
SaveFileDialog())
try
{
//可用文件扩展名
sfdSave.Filter =XML(* .xml *)| * .xml *;
//显示SaveFileDialog
if(sfdSave.ShowDialog()== DialogResult.OK&& sfdSave.FileNa me.Length> 0)
{
////使用原始文件名并在
后添加-NEG// FileStream fs =(FileStream)saveFileDialog1.OpenFile();
//将文件另存为xml
UTF8Encoding utf8 = new UTF8Encoding();
StreamWriter sw = new System.IO.StreamWriter(sfdSave.FileName,false,utf8);
sw.Write(strsave);
sw.Close();
}
}
catch(异常errorMsg)
{
MessageBox.Show(errorMsg.Message);
}

Application.Exit();
}
解决方案



Hi,

Could someone tell me how to retrieve the filename and it's path after a user selected a file on his harddrive please?

What I have tried:

I have nothing for the moment. I'm using an OpenFileDialog object and a SaveFileDialog object. I've read on MSDN about directory.GetFiles but it's not applicable for me. As a matter of fact, all information about GetFiles are for paths with a specific string
(

string[] dirs = Directory.GetFiles(@"c:\", "c*");


or to split the path, or to move folders from 1 directory to another, etc...
And in my case there is no string to give in except for the chosen path of the user.
EDIT

private void btnSavechanges_Click(object sender, EventArgs e)
        {
            //Text from the richtextbox
            string strsave = richTextBox1.ToString();
            //Create a new SaveFileDialog object
            using (SaveFileDialog sfdSave = new SaveFileDialog())
                try
                {
                    //Available file extension
                    sfdSave.Filter = "XML (*.xml*)|*.xml*";
                    //Show SaveFileDialog
                    if (sfdSave.ShowDialog() == DialogResult.OK && sfdSave.FileName.Length > 0)
                    {
                        ////use the original filename and add " -NEG" behind
                        //FileStream fs = (FileStream)saveFileDialog1.OpenFile();
                        //save file as xml
                        UTF8Encoding utf8 = new UTF8Encoding();
                        StreamWriter sw = new System.IO.StreamWriter(sfdSave.FileName, false, utf8);
                        sw.Write(strsave);
                        sw.Close();
                    }
                }
                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }

            Application.Exit();
        }
解决方案



这篇关于单击操作后的Path.getfilename的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 21:20