如何访问c#应用程序目录下的sql文件

时间:2013-12-03 06:11:28

标签: c# sql

我有sql文件,我的c#应用程序需要它们来构建表并插入一些记录。

我将这些sql文件放在应用程序的一个文件夹中(在应用程序目录下)。

如何从c#应用程序访问这些文件,看起来它不存在!

这是测试文件是否存在的代码:

string _myFile = @"\SQL\fileName.sql";

            if (File.Exists(_myFile))
            {
                MessageBox.Show("Exists");
            }
            else
            {
                MessageBox.Show("Not Exists");
            }

应用程序下的文件夹 应用 - > SQL - > fileName.sql

4 个答案:

答案 0 :(得分:1)

否则,如果你使用WinForms,你可以使用

string path = Path.Combine(Application.StartupPath, "mySqlFile.sql");

如果您的文件存储在更深的文件夹中,例如.. \ SQL \ mySqlFile.sql,只需在Path.Combine函数中将其添加为参数。

string path = Path.Combine(Application.StartupPath, "SQL", "mySqlFile.sql");

答案 1 :(得分:0)

如果你想访问

Bin-->Debug--> SQL files

 string sExecutionPath =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); // This will retrun the Debug Folder (i.e)., Exe Execution Path

答案 2 :(得分:0)

只需更改为文件的实际路径,如:

string _myFile = @"C:\SQL\fileName.sql";

答案 3 :(得分:-1)

您可以使用以下代码访问文件

string appLocation = System.IO.Path.GetDirectoryName(
            Assembly.GetExecutingAssembly().Location);
        string xslLocation = System.IO.Path.Combine(appLocation, @"SQL\fileName.sql");
        if (System.IO.File.Exists(xslLocation))
            MessageBox.Show("Exists");
        else
            MessageBox.Show("Not Exists");
相关问题