2016-08-03 94 views
0

您好这是我想要做的,我想.xls名称或转换文件的名称基于原始文件名.dbf。例如我转换acct_code.dbf,excel的文件名应该是acct_code.xls。我尝试concat它,但它没有工作。有人能帮我吗?这个怎么做?C# - 基于原始文件名更改转换文件的文件名

enter image description here

这里是代码。

private void button1_Click(object sender, EventArgs e) 
     { 
      DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. 
      if (result == DialogResult.OK) // Test result. 
      { 
       textBox1.Text = openFileDialog1.FileName; 



      } 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      string constr = "Provider=VFPOLEDB.1;Data Source=" + Directory.GetParent(textBox1.Text).FullName; 
      string ExcelFileName = AppDomain.CurrentDomain.BaseDirectory + "converted_file.xls"; <--- here's the file name for excel file 
      using (OleDbConnection con = new OleDbConnection(constr)) 
      { 
       var sql = "select * from " + Path.GetFileName(textBox1.Text) + ";"; 
       OleDbCommand cmd = new OleDbCommand(sql, con); 
       DataTable dt = new DataTable(); 
       try 
       { 
        con.Open(); 
       } 
       catch (Exception ex) 
       { 

        MessageBox.Show("Error connecting database: " + ex.Message , "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
        return; 
       } 
       if (con.State == ConnectionState.Open) 
       { 
        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        MessageBox.Show("Reading database... ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        da.Fill(dt); 
        MessageBox.Show("Completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
       if (con.State == ConnectionState.Open) 
       { 
        try 
        { 
         con.Close(); 
        } 
        catch { } 
       } 

       if (dt != null && dt.Rows.Count > 0) 
       { 
        GenerateExcel(dt, ExcelFileName); 
       } 
      } 

     } 

回答

2

试试这个:

string originalFile = @"c:\Users\DonyaNenita\Desktop\Copro\install\acct_code.DBF"; 
string newFile = System.IO.Path.GetDirectoryName(originalFile) + "\\" + System.IO.Path.GetFileNameWithoutExtension(originalFile) + ".xls"; 
+0

'System.IO.Path.GetFileNameWithoutExtension(originalFile)'是关键。感谢兄弟:) – nethken

+1

使用System.IO.Path.Combine(directoryName,fileName)而不是directoryName +“\\”+ fileName – Den

1

有一个在Path类中的方法如果您重命名一个文件,然后试试这个,将做到这

string oldFile = @"C:\folder1\folder2\file.dbf"; 
string newFile = Path.ChangeExtension(oldfile, ".xls"); 
// newFile is now C:\folder1\folder2\file.xls 
1

System.IO.File.Move("oldfile.txt", "newfile.log"); 

但是,如果你想要一个函数重新命名文件然后使用:

public void RenameFile(string path, string name) 
{ 
    System.IO.File.Move(Path.GetDirectoryName(filepath) + name + Path.GetExtension(filepath); 
} 

如果你只是想要一个新的名称,然后使用此:

string NewFileName = Path.GetDirectoryName(filepath) + "newfilename" + Path.GetExtension(filepath); 

又一次的功能:

public string GetPathWithNewName(string path, string newname) 
{ 
    return Path.GetDirectoryName(path) + newname + Path.GetExtension(path); 
} 
+0

这不回答问题,OP只是想生成新的文件名 –

1

您也可以使用GetFileWithoutExtension方法:

string basename = System.IO.Path.GetFileNameWithoutExtension("foo.DBF"); 
string newFile = string.Join(".", basename, "xls"); 

我不是确定如果你正在重命名或制作一个新文件...