none
Why do i get this error? RRS feed

  • Question

  • I have a method for opening and reading text files and keep getting this error;

    System.IO.FileNotFoundException: 'Could not find file 'D:\PROGRAMS_IN_PROGRESS\DirCompareForm\DirCompareForm\bin\Debug\dbo.cq_BetsPerRegion.StoredProcedure.sql'.'

    Here is my code;

    Dictionary<string, byte[]> PgiFile = new Dictionary<string, byte[]>();

    private void OpenFolder1() { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { string dir1Name = folderBrowserDialog1.SelectedPath; _PgiFileName = Path.GetFileNameWithoutExtension(folderBrowserDialog1.SelectedPath); DirectoryInfo dirInfo = new DirectoryInfo(dir1Name); foreach(FileInfo name in dirInfo.GetFiles()) { if (FileIsWanted(name.Name)) { byte[] hash = GetFileHash(name.Name); PgiFile.Add(name.Name, hash); } } } } private bool FileIsWanted(string pFile) { if (pFile.StartsWith("dbo.") && (pFile.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase))) { return true; } return false; } private readonly HashAlgorithm _hashAlgorithm = HashAlgorithm.Create(); private byte[] GetFileHash(string fileName) { using (FileStream stream = File.OpenRead(fileName)) { if (checkBox1.Checked) { string contents = File.ReadAllText(fileName); contents = Regex.Replace(contents, @"\s+", string.Empty); byte[] hashedContents = _hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(contents)); return _hashAlgorithm.ComputeHash(hashedContents); } else return _hashAlgorithm.ComputeHash(stream); }

    if i don't put the files in the debug folder they won't open


    CuriousCoder

    Thursday, November 16, 2017 5:15 PM

Answers

  • Hello CuriousCoder15,

    In your OpenFolder1 method, Use the following statement to replace the original code.

     byte[] hash = GetFileHash(name.FullName);

    The "name.Name" just return the fileName rather than full path. If you use file name it will default locate the program's debug folder acquiescently and then search the same name file.

    Sincerely,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CuriousCoder15 Friday, November 17, 2017 9:57 AM
    Friday, November 17, 2017 7:32 AM
    Moderator

All replies

  • if (pFile.StartsWith("dbo.") && (pFile.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase)))
    			{
    				return true;
    			}
    			return false;

    I'm fairly confident you do not want the trailing dot in "dbo."   But it might be correct if your files are actually named something like "dbo.*.sql".

    /regards





    • Edited by Rah1976 Thursday, November 16, 2017 5:48 PM
    Thursday, November 16, 2017 5:45 PM
  • No, the fileIsWanted method works fine, the problem is with the GetFileHash function when I pass the parameter of the file I want to Open.Read it says it can't find the file.

    If I store the files in my debug folder it all works.


    CuriousCoder

    Thursday, November 16, 2017 7:22 PM
  • Hello CuriousCoder15,

    In your OpenFolder1 method, Use the following statement to replace the original code.

     byte[] hash = GetFileHash(name.FullName);

    The "name.Name" just return the fileName rather than full path. If you use file name it will default locate the program's debug folder acquiescently and then search the same name file.

    Sincerely,

    Fei Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CuriousCoder15 Friday, November 17, 2017 9:57 AM
    Friday, November 17, 2017 7:32 AM
    Moderator
  • Hi, thanks.

    Here is what I did;

    Made a variable called _qualifiedPath;

    Two strings for path1 & path2

    used Path.combine(path1, path2)

    Called byte[] hash = GetFileHash(_qualifiedPath)

    Works perfectly, thanks for putting me on the right track.


    CuriousCoder

    Friday, November 17, 2017 9:56 AM