Ok mon problème est, quand j'essaye d'accéder à un fichier pendant que le nom de fichier du chemin provient d'un DataTable il ne trouve pas le fichier.C# - Impossible de trouver le fichier quand une partie de la chaîne de chemin de DataTable
je l'ai déjà testé quand je parse le nom de fichier d'un fichier texte ou tout simplement hardcode dans la chaîne ... et bien sûr ce travail> _ <
juste ne pas arriver là où la diff est quand i chaîne le nom du fichier de la DataTable.
La chaîne il construire ressembler à ceci:
C: \ system Server \/somefile.dat
Voici le code:
string accountConnectionString = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LoadFileChecks();
}
public SqlConnection GetAccountConnection()
{
SqlConnection connection = new SqlConnection(accountConnectionString);
connection.Open();
return connection;
}
public DataTable getFilecheck()
{
using (var con = GetAccountConnection())
{
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tFilecheck", con);
DataTable ds = new DataTable("Filecheck");
da.Fill(ds);
con.Close();
return ds;
}
}
public void LoadFileChecks()
{
DataTable table = getFilecheck();
string localPath = Application.StartupPath;
foreach (DataRow row in table.Rows)
{
string line = row["sFilename"].ToString();
string FilePath = localPath + "\\" + line;
if (!File.Exists(FilePath))
{
MessageBox.Show("File not found");
continue;
}
}
}
Au lieu d'essayer manuellement concaténer les chemins et les noms de fichiers et munge les doubles barres obliques, rechercher la méthode [Path.Combine] (http: // msdn .microsoft.com/fr-fr/library/fyy7a5kt (v = vs.110) .aspx), qui fait le gros du travail pour vous. –
OK essayé cela, la chaîne ressemble à ça maintenant: "C: \\ Server \\ système \ somefile.dat" mais ne peut toujours pas trouver le fichier: < – Saschanski