Reputation: 25
I know it's been asked a lot already, but I still can't get my code to work.
I built a little programm which lets you choose a product and asks for your information if you want to buy it. Every time the given information like name, address and so on... is saved in a single Excel file.
Now I want this file to be created if it doesn't already exists. It works to create one, and overwrite it. But if it exists, File.Exist
will return a false anyway and overwrite the entire file with a new blank. It get's created to [user]\Documents
folder.
My idea to check if the file already exists was this:
if (!File.Exists(@"C:\Bestellungen.xlsx"))
{
CreateNewHistory(); // Method which creates the file in correct format
}
WriteData(); // Method to write given information to correct cells
The method for creating the file is simply:
private void CreateNewHistory()
{
Excel excel = new Excel();
excel.CreateNewFile();
excel.SaveAs(@"Bestellungen.xlsx");
...
// some cells get writte here, just for format
...
excel.Close();
}
"Bestellungen" is German for "orders"... ;)
I suppose it has something to do with the way I pass the path into File.Exist
but I am just clueless. I tried to copy the file into the bin\debug folder of my project. If it's there it works perfectly, but i really would like to know how i can find the file in the documents folder. I already tried put the absolute path, but it's always returning false.
If you need more information about the code let me know, I'm really new to c# or coding basically.
Upvotes: 1
Views: 880
Reputation: 1
its throw a exception due to permission issue please change directory or give proper permission
Upvotes: -1
Reputation: 131189
You're saving the file using a relative path. The file will be saved in the program's working directory. Using C:\
will never work. In fact, you can't even write to the root folder without elevated privileges. Program Files
is another folder you can't (and shouldn't even try) to save into, since 1995.
Older Windows versions would just throw an exception. Some developers would try to force permissions instead of fix their bugs, so newer versions redirect inappropriate writes to eg Documents
or APPDATA
. I suspect you run your program from Program Files
and the OS redirected the file save operation to Documents
.
You should use an absolute path to save files in the correct folder. User documents go to Documents
, application files go to APPDATA
etc. You can get the absolute path to each of those locations with Environment.GetFolderPath, and one of the SpecialFolder values like Desktop
or MyDocuments
eg :
var docFolder=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filePath=Path.Combine(docFolder,"Bestellungen.xlsx");
excel.SaveAs(filePath);
And retrieve it the same way:
var docFolder=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var filePath=Path.Combine(docFolder,"Bestellungen.xlsx");
if (!File.Exists(filePath))
...
The SpecialFolder
enumeration explains the purpose of the various folders:
MyDocuments
contains document files for the current user, Desktop
points to the current user's desktopApplicationData
and LocalApplicatinoData
contain application-specific files used by the current user, like logs and settings etc.CommonDocuments
, CommonDesktop
, CommonApplicationData
contain files used by all users on a computerUpvotes: 4
Reputation: 2538
Use the Document folder to save your file.
Get the User Document Folder
String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Set the Filename
string file="Bestellungen.xlsx";
Finally, generate your actual path
String absolutePath = System.IO.Path.Combine(path , file);
Upvotes: 0