Delete a file with vb.net which is open in another process - vb.net

I create a .txt file in C++ in the application folder /Common/Send/Test.txt
Than I call a VB.net application from the c++ code
the Vb.net app read the test.txt content than send it to a Web-Server with POST request, than delete the file.
But its not delete the file, the VB.net app crashing when it need to delete the file.
My.Computer.FileSystem.DeleteFile(".\\Common\Send\Test.txt")
I am using this to delete the file, what can be the problem?
The problem is its write another program using this file, thats why it cannot delete, how can I stop my application 1st part? Because I read the file, but than its doesnt close it. I think thats the problem.

I would use something like this to delete the file ...
Try
System.IO.File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\Common\Send\Test.txt")
Catch ex As Exception
'it may not exist so no worries ...
End Try
To address the issue of it being open in a different process, here are your options:
http://www.codeproject.com/Questions/661082/Delete-a-file-that-is-in-use-by-another-process-wi

Related

SSIS foreach loop, move each file to folder Done or Error based on any errors during processing

I'm working on an Integration Services project, it will handle a few files every day, process them and either move them to a Done folder or an Error folder, depending on if there were errors while processing the file.
I haven't worked much with SSIS before, only on simple data flows, but I've managed to iterate through all files and process them using this answer. I've added an File System Task which moves the file to the Done folder, based on the DestinationFullPath variable. This all works, and when there aren't any errors, all files are processed and moved to the Done folder.
However, when there is an error in one file (like a missing column, error when storing in database, patientid not found etc.) the entire flow stops.
What I want, is after processing one file it should catch any errors and either move the file to a "Done" folder or to an "Error" folder, and then just continue with the next file.
Is something like this possible, if so how? I've tried changing the DestinationFullPath variable and set it to the Error folder, but then it still throws an error and stops the flow for other files.
Also, the Data Flow Task "Handle next CSV file" (inside the Foreach Loop Container) doesn't have a red Error Output arrow, is that correct? Can I add it somehow?
There are few workarounds:
1. Create another Control flow task For ex: Move Error files and connect Green Precedent from Handle next CSV file to new component, right click on constraint and click on Failure(Red constraint)
2.Handle error within data flow task by utilizing on error property of data flow components, please make sure to keep Delay validation to True on Data flow component so that it doesn't fail on startup.

Delete VBA module

I was deleting an Access Object (a report) and Access crashed during the delete.
The object no longer exists in Access, but its module still shows up in VBA like a ghost.
If I click on it, I get a FILE NOT FOUND error.
If I try to compact & repair or compile the database, I get a FILE NOT FOUND error.
How can I solve this problem?
A potential option is to create a new database file, and import all of the content from the old database file. that will clear out the funky ghost stuff.
you write you tried compile already and it did not work.
Did you try decompile first though?
First of all make a backup copy (but am sure you already did that :) )
With Access and your access file closed type from the command prompt:
C:\yourOfficeInstallPath\MSACCESS.EXE /decompile
Access will start. Click File > Open and select the database you want to decompile
Open any module and click Debug > Compile
Then save your file and close.
Open again your file and compact it.
Let me know if it solved it.

Exception trying to Start a process after copying the file

I'm getting an annoying error like:
The process cannot access the file 'C:\Program Files (x86)\AceHc\trfpt.exe' because it is being used by another process.
The error happens when I try to use Process.Start after File.Copy the same file.
Code:
File.Copy(PathFrom & "\trfpt.exe", PathTo & "\trfpt.exe", True)
Process.Start(PathTo & "\trfpt.exe")
What am I doing wrong?
Do you have a virus scanner that might be scanning the file directly after the copy and blocking it?
It might be worth trying to temporarily disable it and see if the problem goes away.
Also, depending in the size of the file, maybe the EXE file is still being copied (that is, the copy function doesn't block que program flow, your code continue and the copy process continue in background).
Download Handle from Windows Sysinternals and run it as admin from cmd to get a list of processes which hold a handle on that file:
handle.exe trfpt.exe
A wild guess - do you have a Windows Explorer window open looking at the folder 'Pathto' ?

Setting my application to open my custom file type?

In my application I have created my own filetype, basically all it is is a normal text file with my own extension on it. Now my question is I want to be able to have my application open up these files on the users computer. So when the user clicks the file they saved from my application it will open up in only my application. I am going to be using clickonce to deploy my application, does it have this functionality in it already? My application is in vb.net.
You may have a look at How to: Create a File Association For a ClickOnce Application. Within the site there is a description how to set it manually. Within VS you may set it by following these steps:
open context menu of project (right click project in solution explorer)
go to Publish
Options
File Associations
There you can set all necessary assocs. You should also check out ClickOnce File Association on SO! (Note: access fileName to open by AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData)
You can find the answer here, is about creating file associations for a click once application.
Try this.
Hope it has everything you want.
Visit http://www.c-sharpcorner.com/UploadFile/scottlysle/CustomFileType12082006000848AM/CustomFileType.aspx
your Question consists of tow parts (I guess)
the first is how to create file association and the second is how to open the file when the app runs
to answer the first one
file association VB.NET
and the second one when you open the file you saved with you're app:
If My.Application.CommandLineArgs < 0 Then
MessageBox.Show("No File dragged into the application's executable!")
Else
Messagebox.Show("File " & My.Application.CommandLineArgs(0) & " is ready to open!")
End If
you can use
My.Application.CommandLineArgs(0)
as file path
best wishes

VB.NET: Detect if a text file is open

Is there any way to determine if a text file is currently open in a text editor? Or better yet, is there a way to trigger an event when a text file is opened (from any program)?
Using the FileSystemWatcher component you can detect Changed, Created, Deleted, and Renamed events statically.
If you want to detect Last Access you need to manually set the NotifyFilter to include LastAccess.
When most editors have a file open they typically follow a set strategy:
1. Open File
2.Read entire contents into buffer
3.Close File
Then your program runs. Since the file is already closed, any attempts to open it will of course succeed. Using a FileSystemWatcher this can be detected if a file is open or closed. However, you will not be able to detect if the file has been open prior to your program running.
I think I'll make a Timer that checks the last modified time of the file.... and when the program starts, it'll get the last modified time.