C# excel SaveAs file could not be accessed when saving -


attempting save excel worksheet programmatically unique filename in loop.

    private static datetime csvtime = datetime.now;     private static string time = csvtime.tostring("hh:mm:ss");     private static string path = @"c:\users\user\desktop\monetaryentry_"+time+".csv";     mysheet = (excel.worksheet)mybook.worksheets[1];   mysheet.saveas(path, microsoft.office.interop.excel.xlfileformat.xlcsv); 

this fails following error

an unhandled exception of type 'system.runtime.interopservices.comexception' occurred in monetaryentryformatting.exe

additional information: file not accessed. try 1 of following:

• make sure specified folder exists.

• make sure folder contains file not read-only.

• make sure file name not contain of following characters: < > ? [ ] : | or *

• make sure file/path name doesn't contain more 218 characters.

however when change path string to

private static string path = "@c:\users\user\desktop\monetaryentry_.csv"; 

it resolves.

the goal iterate through loop saving each time unique *.csv file. undetermined amount of .csv files generated each time.

a few notes;

writing desktop user account i'm doing work in. if relevant, copy file on desktop , paste different name , work there.

i feel might missing simple kind of issue typically is..

private static datetime csvtime = datetime.now; private static string time = csvtime.tostring("hh_mm_ss"); private static string path = @"c:\users\user\desktop\monetaryentry_"+time+".csv"; mysheet = (excel.worksheet)mybook.worksheets[1]; mysheet.saveas(path, microsoft.office.interop.excel.xlfileformat.xlcsv); 

this going work change tostring. : not character allowed in path. (look @ 3rd suggestion of error)


Comments